读书人

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

发布时间: 2012-03-24 14:00:46 作者: rapoo

??? 简单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 行受影响)**/ 

读书人网 >SQL Server

热点推荐