SQL - 从一个表中查找另一个表中不存在的记录

时间:2008-12-15 09:33:10

标签: sql mysql

我有以下两个SQL表(在MySQL中):

Phone_book
+----+------+--------------+
| id | name | phone_number |
+----+------+--------------+
| 1  | John | 111111111111 |
+----+------+--------------+
| 2  | Jane | 222222222222 |
+----+------+--------------+

Call
+----+------+--------------+
| id | date | phone_number |
+----+------+--------------+
| 1  | 0945 | 111111111111 |
+----+------+--------------+
| 2  | 0950 | 222222222222 |
+----+------+--------------+
| 3  | 1045 | 333333333333 |
+----+------+--------------+

如何找出phone_number不在Phone_book的人拨打的电话?所需的输出是:

Call
+----+------+--------------+
| id | date | phone_number |
+----+------+--------------+
| 3  | 1045 | 333333333333 |
+----+------+--------------+

非常感谢任何帮助。

8 个答案:

答案 0 :(得分:374)

有几种不同的方法可以实现这一点,效率会有所不同,具体取决于查询优化器的优异程度以及两个表的相对大小:

这是最短的陈述,如果您的电话簿很短,可能会最快:

SELECT  *
FROM    Call
WHERE   phone_number NOT IN (SELECT phone_number FROM Phone_book)

或(感谢Alterlife

SELECT *
FROM   Call
WHERE  NOT EXISTS
  (SELECT *
   FROM   Phone_book
   WHERE  Phone_book.phone_number = Call.phone_number)

或(感谢WOPR)

SELECT * 
FROM   Call
LEFT OUTER JOIN Phone_Book
  ON (Call.phone_number = Phone_book.phone_number)
  WHERE Phone_book.phone_number IS NULL

(忽略这一点,正如其他人所说,通常最好只选择你想要的列,而不是'*')

答案 1 :(得分:75)

SELECT Call.ID, Call.date, Call.phone_number 
FROM Call 
LEFT OUTER JOIN Phone_Book 
  ON (Call.phone_number=Phone_book.phone_number) 
  WHERE Phone_book.phone_number IS NULL

应该删除子查询,允许查询优化器发挥其魔力。

另外,避免使用“SELECT *”,因为如果有人改变了基础表或视图,它会破坏你的代码(并且效率很低)。

答案 2 :(得分:23)

下面的代码在处理更大的数据集时会比上面提到的答案更有效。

SELECT * FROM Call WHERE 
NOT EXISTS (SELECT 'x' FROM Phone_book where 
Phone_book.phone_number = Call.phone_number)

答案 3 :(得分:5)

SELECT DISTINCT Call.id 
FROM Call 
LEFT OUTER JOIN Phone_book USING (id) 
WHERE Phone_book.id IS NULL

这将返回Phone_book表中缺少的额外id-s。

答案 4 :(得分:3)

我认为

SELECT CALL.* FROM CALL LEFT JOIN Phone_book ON 
CALL.id = Phone_book.id WHERE Phone_book.name IS NULL

答案 5 :(得分:2)

SELECT t1.ColumnID,
CASE 
    WHEN NOT EXISTS( SELECT t2.FieldText  
                     FROM Table t2 
                     WHERE t2.ColumnID = t1.ColumnID) 
    THEN t1.FieldText
    ELSE t2.FieldText
END FieldText       
FROM Table1 t1, Table2 t2

答案 6 :(得分:1)

SELECT name, phone_number FROM Call a
WHERE a.phone_number NOT IN (SELECT b.phone_number FROM Phone_book b)

答案 7 :(得分:1)

可替换地,

select id from call
minus
select id from phone_number