当前位置: 代码迷 >> Sql Server >> 急请教这个SQL语句要如何写
  详细解决方案

急请教这个SQL语句要如何写

热度:8   发布时间:2016-04-27 13:19:37.0
急,请问这个SQL语句要怎么写?
有A表和B表,以下为表内容。
A表
a b c
001 111 222
002 ccc 333

B表
a b c
003 111 222
002 ccc 333

如何查询出A表和B表中不相同的记录,查询结果应该是:
结果表
a b c
001 111 222

请问这样的SQL语句要怎么写?一个SQL语句可以完成吗?

------解决方案--------------------
SQL code
SELECT A.*FROM A LEFT JOIN BON A.A= B.AAND A.B = B.BAND A.C = B.CWHERE B.A IS NULL
------解决方案--------------------
SQL code
create table a(    a    varchar(10),    b    varchar(10),    c    int)create table b(    a    varchar(10),    b    varchar(10),    c    int)insert into a values('001','111',222)insert into a values('002','ccc',222)insert into b values('003','111',222)insert into b values('002','ccc',333)select a.* from a where a not in (select a from b)/*a,b,c001,111,222(1 行受影响)
------解决方案--------------------
SQL code
create table a(a varchar(10),b varchar(10),c int)gocreate table b(a varchar(10),b varchar(10),c int)goinsert into a select '001', '111', 222insert into a select '002', 'ccc', 333insert into b select '003', '111', 222insert into b select '002', 'ccc', 333select * from aexceptselect * from b---------a    b    c001    111    222
------解决方案--------------------
SQL code
use Tempdbgo--> -->  if not object_id(N'Tempdb..#A') is null    drop table #AGoCreate table #A([a] nvarchar(3),[b] nvarchar(3),[c] int)Insert #Aselect N'001',N'111',222 union allselect N'002',N'ccc',333Gouse Tempdbgo--> -->  if not object_id(N'Tempdb..#B') is null    drop table #BGoCreate table #B([a] nvarchar(3),[b] nvarchar(3),[c] int)Insert #Bselect N'003',N'111',222 union allselect N'002',N'ccc',333GoSELECT * FROM (SELECT * FROM #A EXCEPT Select * from #B)t UNION allSELECT * FROM (SELECT * FROM #B EXCEPT Select * from #A)T/*a    b    c001    111    222003    111    222*/
  相关解决方案