日期:2014-05-18 浏览次数:21013 次
select a.id ,a.[type] ,b.class ,c.[name] from a left join b on a.id=b.id left join (select *,rn=row_number() over(partition by id order by getdate())) c on a.id=c.id and rn=1 where a.id=1
------解决方案--------------------
更改c表的重复ID编号使之唯一 ,查找重复的编码
select id,count(id) from c group by id
或者加个like 语句过滤下
select a.id ,a.[type] ,b.class ,c.[name] from a left join b on a.id=b.id left join c on a.id=c.id where a.id=1 and c.name like '%s%'
------解决方案--------------------
CREATE TABLE #A表 (ID INT, TYPEA CHAR(10)) INSERT #A表 SELECT '1','1' UNION ALL SELECT '2','1' UNION ALL SELECT '3','2' CREATE TABLE #B表 (ID INT, CLASS CHAR(10)) INSERT #B表 SELECT '1','1' UNION ALL SELECT '2','2' UNION ALL SELECT '4','4' UNION ALL SELECT '55','55' UNION ALL SELECT '66','66' CREATE TABLE #C表 (ID INT, NAMEC CHAR(10)) INSERT #C表 SELECT '1','dfs1111d' UNION ALL SELECT '2','kkk2Ok' UNION ALL SELECT '3','dd3OK' UNION ALL SELECT '1','111ddd' DROP TABLE #b表 SELECT TOP 1 A.ID,A.TYPEA,B.CLASS,C.NAMEC FROM #A表 A,#B表 B,#C表 C WHERE A.ID=B.ID AND B.ID=C.ID AND A.ID='1' /* C表中的ID值重复了,故有两条记录 ID TYPEA CLASS NAMEC ----------- ---------- ---------- ---------- 1 1 1 dfs1111d */