有两张表:一张A表he一张B表 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 :right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录:inner join(等值连接) 只返回两个表中联结字段相等的行: 表A数据: 表B数据: 1.查询两张表中都有的记录: sql: SELECT a.* FROM a INNER JOIN b ON a.a_id = b.b_id; 2.查询表A中有,表B中没有的数据: sql: SELECT a.
Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId | int | | FirstName | varchar | | LastName | varchar | +-------------+---------+ PersonId is the primary key column for this table. Table: Address +--
需要查两个表之间的差集 首先,想到的是主键直接not in select mailbox_id from co_user where mailbox_id not in (select mailbox_id from core_mailbox); 好吧!这个是可以,但是数据多了的话,想到这个查询的逻辑有点受不住 于是再改为下面的这样: select cu.mailbox_id,cm.mailbox_id from co_user as cu left join core_mailbox as c
Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId | int | | FirstName | varchar | | LastName | varchar | +-------------+---------+ PersonId is the primary key column for this table. Table: Address +--
A.B两张表,找出ID字段中,存在A表,但是不存在B表的数据,A表总共13W数据,去重后大约3万条数据,B表有2W条数据,且B表的ID有索引. 方法一 使用not in,容易理解,效率低. select distinct a.id from a where a.id not in(select id from b) 1 方法二 使用left join … on ….,’b.id is null’,表示左连接之后在b.id字段为null的记录 select a.id from a left joi