您的位置:程序门 -> delphi -> 数据库相关



查询没有出现过的内容


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


查询没有出现过的内容[已结贴,结贴人:kiss74]
发表于:2007-09-22 12:17:20 楼主
表1
编号   名称
1       n1
2       n2
3       n3
4       n4

表2
编号   名称   客户
1         n1     amc
2         n2     ampag
3         n4     fortune

请问如何在表1中查询出在表2没有出现过的名称?
发表于:2007-09-22 15:32:211楼 得分:0
declare   @t1   table(id   int,names   varchar(10))
insert   @t1   select   1,'n1'
union   all     select   2,'n2'
union   all     select   3,'n3'
union   all     select   4,'n4'

declare   @t2   table(id   int,names   varchar(10),customer   varchar(50))
insert   @t2   select   1,'n1','amc'
union   all     select   2,'n2','ampag'
union   all     select   3,'n4','fortune'

select   *
from   @t1   a
where   not   exists(select   1   from   @t2   where   a.names=names)
发表于:2007-09-23 12:31:342楼 得分:0
sql code
select * from 表1 where 名称 not inselect 名称 from 表2)
发表于:2007-09-23 12:38:543楼 得分:0
sql code
select * from 表1 where not existsselect * from 表2 where 表1.name=name)
发表于:2007-09-24 11:41:394楼 得分:0
搞定了,谢谢二位!


快速检索

最新资讯
热门点击