Reputation: 10693
I have similar table structure as shown below
Now here I want write a query (without using inner query) and find all those number who have condition like (value 1 = A and value 2 = B) and (value 1 = B and value 2 = A).
Means Kind of vice versa case where a number is having both value1 and value2 having A and B.
Thus for the given case my query output would be 1 and 4.
Upvotes: 0
Views: 1770
Reputation: 2715
SELECT NUMBER FROM MYTABLE WHERE value1 = 'A' and value2 = 'B'
INTERSECT
SELECT NUMBER FROM MYTABLE WHERE value2 = 'A' and value1 = 'B';
Upvotes: 1
Reputation: 123
Please try this query:
select t1.NUMBER from mytable t1
join mytable t2 on t1.NUMBER = t2.NUMBER
where t1.Value1 = 'A' and t1.Value2 = 'B' and t2.Value1 = 'B' and t2.Value2 = 'A'
You can check the result: http://sqlfiddle.com/#!2/a5e7ae/1
Upvotes: 2
Reputation: 23992
If you don't want to repeat the duplicate rows, following should work:
mysql> select a.* from tbl_so_q23676640 a
-> join tbl_so_q23676640 b
-> on a.v1 = b.v2 and a.v2 = b.v1
-> where a.n=b.n
-> group by a.n, a.v1, a.v2
-> ;
+------+------+------+
| n | v1 | v2 |
+------+------+------+
| 1 | a | b |
| 1 | b | a |
| 4 | a | b |
| 4 | b | a |
+------+------+------+
4 rows in set (0.00 sec)
Upvotes: 1