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
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
You can try this:
SELECT id FROM table1
where id not in (SELECT id FROM table2)
MYSQL does not support EXCEPT
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 )
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
.
there is no keyword as 'except' in mysql.
See: Error when using except in a query
for the alternate way using not in
and exists
You can use Left Join
, as following:
SELECT t1.id
FROM table1 AS t1 LEFT JOIN table2 AS t2
ON t1.id = t2.id
WHERE t2.id IS NULL
For more info, You can go to MySQL MINUS (or Except)