2017-05-30 46 views
1

如何写一个MySQL查询得到在左表中的记录。
左加入让我从T1表中的所有记录,并有在MySQL中没有MINUSMySQL查询只检索记录在左表

only left

编辑:
不想使用子查询

+0

类似的问题已经存在,我相信: [从另一个减去表] (https://stackoverflow.com/questions/5738240/mysql-subtract-a-table-from-another) – Siddharth

回答

4

使用not exists

select l.* 
from `left` l 
where not exists (select 1 from `right` r where r.id = l.id); 

如果您需要更多列的比较,可以扩大逻辑:

select l.* 
from `left` l 
where not exists (select 1 
        from `right` r 
        where r.col1 = l.col1 and 
         r.col2 = l.col2 and 
         . . . 
       ); 
+0

竟是寻找没有任何子queries.Thank您的查询。 – Pradeep