当前位置: 代码迷 >> Sql Server >> ? 简单SQL语句查询 ?解决思路
  详细解决方案

? 简单SQL语句查询 ?解决思路

热度:103   发布时间:2016-04-27 14:10:27.0
??? 简单SQL语句查询 ???
Table1
单号 名称
1 a1
1 a2
1 a3
2 b1

Table2
单号 名称
1 a1

如何查询出单号等于1,Table1中不在Table2的记录
查询结果Table:

单号 名称
1 a2
1 a3



------解决方案--------------------
SQL code
if object_id('[Table1]') is not null drop table [Table1]gocreate table [Table1]([单号] int,[名称] varchar(2))insert [Table1]select 1,'a1' union allselect 1,'a2' union allselect 1,'a3' union allselect 2,'b1'goif object_id('[Table2]') is not null drop table [Table2]gocreate table [Table2]([单号] int,[名称] varchar(2))insert [Table2]select 1,'a1'goselect a.*from table1 aleft join table2 b on a.单号=b.单号 and a.名称=b.名称where a.单号=1and b.名称 is null/**单号          名称----------- ----1           a21           a3(2 行受影响)**/
  相关解决方案