4

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
Dino
  • 65
  • 1
  • 4

5 Answers5

8

You can try this:

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

MYSQL does not support EXCEPT

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

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 ) 
Asit
  • 458
  • 4
  • 14
1

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.

Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
Kaushik
  • 2,072
  • 1
  • 23
  • 31
0

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

Community
  • 1
  • 1
vijay
  • 494
  • 5
  • 17
0

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)