Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Except not working in mysql?

Tags:

mysql

I am trying to get id's from a specific table except from those ids from another table and it does not work:

SELECT id FROM table1 
EXCEPT 
SELECT id FROM table2
like image 231
Dino Avatar asked Mar 23 '15 12:03

Dino


3 Answers

You can try this:

SELECT id FROM table1 
where id not in (SELECT id FROM table2)

MYSQL does not support EXCEPT

like image 123
Rahul Tripathi Avatar answered Nov 14 '22 02:11

Rahul Tripathi


I don't think mysql supports EXCEPT. Instead try this way :

SELECT t1.id 
FROM table1 t1 
WHERE NOT EXISTS 
    ( SELECT 0 
      FROM table2 t2 
      WHERE t2.id = t1.id ) 
like image 35
Asit Avatar answered Nov 14 '22 01:11

Asit


Use Not IN

SELECT `id` FROM `table1` 
WHERE `id` NOT IN (SELECT `id` FROM `table2`);

For more information you can take a look at MySQL NOT IN() function.

You can also use Left Join.

like image 1
Kaushik Avatar answered Nov 14 '22 03:11

Kaushik