实在搞不定,2表通过第3个表关联,取出不重复记录

路人乙e 2010-02-04 12:02:23
表A(cid对应表C.id1)
id name cid
1 aa 1
2 bb 1
3 cc 2
4 dd 3

表B(cid对应表C.id2)
id name cid
100 abc 11
101 bcd 11
102 cde 12
103 def 13

表C
id1 id2
1 11
2 12
3 13

select a.id,b.id,a.cid
from a,b,c
where a.cid=c.id1 and b.cid=c.id2

这样查询的结果是
a.id b.id cid
1 100 1
1 101 1(此行不该有,因为a.id=1已经匹配过了)
2 100 1(此行不该有,因为b.id=100已经匹配过了)
2 101 1
3 102 2
4 103 3

但我要的结果是
a.id b.id cid
1 100 1
2 101 1
3 102 2
4 103 3

解决了再开2贴送200
...全文
351 40 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
40 条回复
切换为时间正序
请发表友善的回复…
发表回复
enan608 2010-12-21
  • 打赏
  • 举报
回复
还是看不到答案啊
ldftopgun 2010-02-04
  • 打赏
  • 举报
回复
left join
--小F-- 2010-02-04
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 josy 的回复:]
引用 25 楼 fredrickhu 的回复:
引用 24 楼 happyflystone 的回复:
2005
SQL codeselect a.id,(b.id),a.cidfrom (select*,cnt=row_number()over(partitionby cidorderby id)from a  )aleftjoin con a.cid=c.id1leftjoin (select*,cnt=row_number()over(partitionby cidorderby id)from b  )bon b.cid=c.id2and a.cnt= b.cnt


挖哈哈 被我抄袭了 你慢了


大概你没发现,你的结果错了
[/Quote]

秘密 只是秘密 不要随便说
--小F-- 2010-02-04
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 josy 的回复:]
引用 25 楼 fredrickhu 的回复:
引用 24 楼 happyflystone 的回复:
2005
SQL codeselect a.id,(b.id),a.cidfrom (select*,cnt=row_number()over(partitionby cidorderby id)from a  )aleftjoin con a.cid=c.id1leftjoin (select*,cnt=row_number()over(partitionby cidorderby id)from b  )bon b.cid=c.id2and a.cnt= b.cnt


挖哈哈 被我抄袭了 你慢了


大概你没发现,你的结果错了
[/Quote]

555555555555555
百年树人 2010-02-04
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 fredrickhu 的回复:]
引用 24 楼 happyflystone 的回复:
2005
SQL codeselect a.id,(b.id),a.cidfrom (select*,cnt=row_number()over(partitionby cidorderby id)from a  )aleftjoin con a.cid=c.id1leftjoin (select*,cnt=row_number()over(partitionby cidorderby id)from b  )bon b.cid=c.id2and a.cnt= b.cnt


挖哈哈 被我抄袭了 你慢了
[/Quote]

大概你没发现,你的结果错了
--小F-- 2010-02-04
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 happyflystone 的回复:]
2005
SQL codeselect a.id,(b.id),a.cidfrom (select*,cnt=row_number()over(partitionby cidorderby id)from a )aleftjoin con a.cid=c.id1leftjoin (select*,cnt=row_number()over(partitionby cidorderby id)from b )bon b.cid=c.id2and a.cnt= b.cnt
[/Quote]

挖哈哈 被我抄袭了 你慢了
-狙击手- 2010-02-04
  • 打赏
  • 举报
回复
2005
select a.id,(b.id),a.cid 
from (select *,cnt=row_number() over(partition by cid order by id) from a )a
left join c on a.cid=c.id1
left join (select *,cnt=row_number() over(partition by cid order by id) from b )b on b.cid=c.id2
and a.cnt = b.cnt
--小F-- 2010-02-04
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 sq_zhuyi 的回复:]
引用 12 楼 happyflystone 的回复:
如果2005可以row_number()

row_number()又如何写?
[/Quote]
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2010-02-04 12:08:09
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[A]
if object_id('[A]') is not null drop table [A]
go
create table [A]([id] int,[name] varchar(2),[cid] int)
insert [A]
select 1,'aa',1 union all
select 2,'bb',1 union all
select 3,'cc',2 union all
select 4,'dd',3
--> 测试数据:[B]
if object_id('[B]') is not null drop table [B]
go
create table [B]([id] int,[name] varchar(3),[cid] int)
insert [B]
select 100,'abc',11 union all
select 101,'bcd',11 union all
select 102,'cde',12 union all
select 103,'def',13
--> 测试数据:[C]
if object_id('[C]') is not null drop table [C]
go
create table [C]([id1] int,[id2] int)
insert [C]
select 1,11 union all
select 2,12 union all
select 3,13
--------------开始查询--------------------------
select
a.id,(b.id),a.cid
from
(select *,cnt=row_number()over(partition by cid order by getdate()) from a )a
left join
c on a.cid=c.id1
left join
(select *,cnt=row_number()over(partition by cid order by getdate()) from b )b on b.cid=c.id2
and
a.cnt = b.cnt

----------------结果----------------------------
/* id id cid
----------- ----------- -----------
1 100 1
2 100 1
3 102 2
4 103 3

(4 行受影响)

*/
-狙击手- 2010-02-04
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 sq_zhuyi 的回复:]
引用 12 楼 happyflystone 的回复:
如果2005可以row_number()

row_number()又如何写?
[/Quote]

生成那个cnt呀,
路人乙e 2010-02-04
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 happyflystone 的回复:]
如果2005可以row_number()
[/Quote]
row_number()又如何写?
快乐_石头 2010-02-04
  • 打赏
  • 举报
回复
關注~
--小F-- 2010-02-04
  • 打赏
  • 举报
回复
晕 下面一票人都正确的
路人乙e 2010-02-04
  • 打赏
  • 举报
回复
11楼的可以!感激涕零
这个先不结贴,以便他人学习,我另开贴送分
送分贴:http://topic.csdn.net/u/20100204/12/0e5b9e86-d984-42be-98df-ec9cbfc6dece.html
路人乙e 2010-02-04
  • 打赏
  • 举报
回复
11楼的结果是对的
我来测试下
-狙击手- 2010-02-04
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 sq_zhuyi 的回复:]
5楼的第二行还是不对呀
[/Quote]

11楼
csuxp2008 2010-02-04
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 sq_zhuyi 的回复:]
引用 3 楼 happyflystone 的回复:
SQL codeselect a.id,min(b.id),a.cidfrom aleftjoin con a.cid=c.id1leftjoin bon b.cid=c.id2groupby a.id,a.cid

结果为:
1 100 1
2 100 1
3 102 2
4 103 3
第二行不对
[/Quote]

那是你没说清楚你想要的结果,对于b表相同的cid,取id大的
路人乙e 2010-02-04
  • 打赏
  • 举报
回复
8楼的结果一样 第2行不对
路人乙e 2010-02-04
  • 打赏
  • 举报
回复
5楼的第二行还是不对呀
-狙击手- 2010-02-04
  • 打赏
  • 举报
回复
如果2005可以row_number()
-狙击手- 2010-02-04
  • 打赏
  • 举报
回复
------------------------------------
-- Author: flystone
-- Version:V1.001
-- Date:2010-02-04 12:08:18
------------------------------------

-- Test Data: A
If object_id('A') is not null
Drop table A
Go
Create table A(id int,name nvarchar(2),cid int)
Go
Insert into A
select 1,'aa',1 union all
select 2,'bb',1 union all
select 3,'cc',2 union all
select 4,'dd',3
Go
-- Test Data: B
If object_id('B') is not null
Drop table B
Go
Create table B(id int,name nvarchar(3),cid int)
Go
Insert into B
select 100,'abc',11 union all
select 101,'bcd',11 union all
select 102,'cde',12 union all
select 103,'def',13
Go
-- Test Data: C
If object_id('C') is not null
Drop table C
Go
Create table C(id1 int,id2 int)
Go
Insert into C
select 1,11 union all
select 2,12 union all
select 3,13
Go
--Start
select a.id,(b.id),a.cid
from (select *,cnt=(select count(1) from a where cid = aa.cid and id <=aa.id ) from a as aa)a
left join c on a.cid=c.id1
left join (select *,cnt=(select count(1) from b where cid = bb.cid and id <=bb.id ) from b as bb)b on b.cid=c.id2
and a.cnt = b.cnt
--group by a.id,a.cid

--Result:
/*


id id cid
----------- ----------- -----------
1 100 1
2 101 1
3 102 2
4 103 3

(所影响的行数为 4 行)
*/
--End
加载更多回复(20)

22,301

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧