首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

帮个忙,SQL~解决方案

2012-01-19 
帮个忙,SQL~~A,B两个表结构完全一样,字段ID,date。要求查询A表中与B表重复记录(id相同且date的年与月相同(

帮个忙,SQL~~
A,   B   两个表结构完全一样,字段ID   ,date。
要求查询A表中与B表重复记录(id相同且date的年与月相同(日可以不同)即算重复)。
字段
ID                   date
1                   2007-2-1
1                 2007-3-1

[解决办法]
select * from a where exists(select 1 from b where a.ID=id and a.date=daye)
[解决办法]
select * from a where exists(select 1 from b where a.ID=id and left(CONVERT(varchar(30),a.date,102),4)=left(CONVERT(varchar(30),date,102),4))
[解决办法]
--try


select * from A
where (select count(*) from B where ID=A.ID
and convert(char(7),date,120)=convert(char(7),A.date,120))> 1
[解决办法]
--try


select * from A
where exists(select 1 from B where ID=A.ID
and convert(char(7),date,120)=convert(char(7),A.date,120))

热点排行