我有两个表,我想在第一个表中只选择第二个表中没有的报告,其中 id_user='N',全部通过外键 id(table1.id,filter_table1.id_table1)。
SELECT *
FROM table1, filter_table1
WHERE table1.id!=(SELECT id_table1 FROM filter_table1 WHERE id_user='1')
我宁愿使用而LEFT JOIN
不是NOT IN
SELECT a.*
FROM table1 a
LEFT JOIN filter_table1 b
ON a.ID = b.id_table1 AND
b.id_user = 1
WHERE b.id_table1 IS NULL
SELECT *
FROM table1
WHERE table1.id NOT IN (SELECT id_table1 FROM filter_table1 WHERE id_user='1')
您不需要在上述查询中加入 - 只需使用所谓的相关子查询:
SELECT t1.*
FROM table1 t1
WHERE t1.id NOT IN (
SELECT id_table1
FROM filter_table1
WHERE id_user = '1' AND t1.id = filter_table1.id_table1
)
尝试这个
SELECT *
FROM table1, filter_table1
WHERE table1.id NOT IN (SELECT id_table1 FROM filter_table1 WHERE id_user='1')