0

嗨,我有这两张桌子

表格1

id    Selection  
-------------------
1     John           
2     Ely               
3     Marcus            
4     Steve           
5     Fritz           
6     Orly           
7     Carlo              
8     Lee    

表 2

id    Selected 
-------------------
1     John                         
3     Marcus 
4     Steve                     
5     Fritz           
7     Carlo 

返回将是未选择的行。这个输出的查询是什么

id    Selection 
-------------------         
2     Ely                         
6     Orly                  
8     Lee
4

3 回答 3

2

用于LEFT JOIN连接两个表和t2.ID IS NULL删除公共记录

SELECT t1.* FROM table1 t1 
  LEFT JOIN table2 t2 
    ON t1.ID = t2.ID 
 WHERE t2.ID IS NULL

输出:

╔════╦═══════════╗
║ ID ║ SELECTION ║
╠════╬═══════════╣
║  2 ║ Ely       ║
║  6 ║ Orly      ║
║  8 ║ Lee       ║
╚════╩═══════════╝

看到这个 SQLFiddle

于 2013-07-18T06:57:58.807 回答
1

您可以使用左连接

 Select t1.id,t2.selection from 
 table1 t1 left join table2 t2 
 ON t1.ID = t2.ID 
 where t2.id is null;
于 2013-07-18T06:58:12.737 回答
0

使用此查询。它为你工作。

select table1.* from table1 where table1.id not in (select id from table2)
于 2013-07-18T08:39:07.120 回答