日期:2014-05-19  浏览次数:20651 次

帮个忙,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))