2

我有两个表,我想在第一个表中只选择第二个表中没有的报告,其中 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') 
4

4 回答 4

2

我宁愿使用而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
于 2013-04-12T12:42:11.450 回答
1
SELECT * 
FROM table1
WHERE table1.id NOT IN (SELECT id_table1 FROM filter_table1 WHERE id_user='1') 
于 2013-04-12T12:39:53.443 回答
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
) 
于 2013-04-12T12:40:12.997 回答
1

尝试这个

SELECT * 
FROM table1, filter_table1 
WHERE table1.id NOT IN (SELECT id_table1 FROM filter_table1 WHERE id_user='1') 
于 2013-04-12T12:41:38.683 回答