例如:
A表中的字段a有40000条数据
B表中的字段a有60000条数据,其中的40000条数据跟A表是一样的
怎样能把那不一样的20000条数据查询出来啊?
--建表table1,table2:
create table table1(id int,name varchar(10)); create table table2(id int,score int); insert into table1 select '1','lee'; insert into table1 select '2','zhang'; insert into table1 select '3','steve'; insert into table1 select '4','wang'; insert into table2 select '1','90'; insert into table2 select '2','100'; insert into table2 select '3','70';
如表
-------------------------------------------------
table1
-------------------------------------------------
id name
1 lee
2 zhang
3 steve
4 wang
-------------------------------------------------
table2
-------------------------------------------------
id score
1 90
2 100
3 70
-------------------------------------------------
(1)左向外联接的结果集包括 left outer 子句中指定的左表的所有行,而不仅仅是联接列所匹配的行。如果左表的某行在右表中没有匹配行,则在相关联的结果集行中右表的所有选择列表列均为空值(null)。
(2)sql语句
select * from table1 t1 left join table2 t2 on t1.id = t2.id
3 steve 3 70-------------结果-------------
id name id score
------------------------------
1 lee 1 90
2 zhang 2 100
4 wang null null
------------------------------
注释:包含table1的所有子句,根据指定条件返回table2相应的字段,不符合的以null显示
(3)那么获取差值
您可能想查找下面的文章: |