Possible Duplicate:
MySQL – How to get a list of values in Table A that don’t exist in Table B?
I have three tables .
Table x:
+------+
| ID |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
+------+
Table Y
+------+
| ID |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+------+
Table Z
+------+
| ID |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
+------+
I have to find the value exists in TABLE Z but it not exists in table X and Y .
Output:
+----+
| ID |
------
| 6 |
| 7 |
+----+
How can I get this?
You could use
EXISTSto make a subquery.@RaphaëlAlthaus’s answer is probably better on efficiency, though. See Can I get better performance using a JOIN or using EXISTS?
The MySQL reference has a section on
EXISTS: 13.2.9.6. Subqueries with EXISTS or NOT EXISTS.