实在搞不定,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
...全文
332 40 打赏 收藏 转发到动态 举报
写回复
用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)
本课程同步书籍:《Python编程从零基础到项目实战》,共500页,其中,电子练习册100页,加上本书400页。同步赠送书籍pdf电子书下载,去每一章第一节或第二节课件下载。赠送电子书:Python编程从零基础到项目实践习题答案及分析.pdfPython内置函数案例演示.pdfPython编程从零基础到项目实战-PPT.rar 电子书目录如下:Python编程从零基础到项目实践习题答案及分析.pdf目录Python内置函数案例演示.pdf包含70个内置函数。Python编程从零基础到项目实战-PPT.rar       第1章 从零开始.pptx    第2章 变量和简单数据类型.pptx    第3章 条件分支与循环.pptx    第4章 列与元组.pptx    第5章 字典.pptx    第6章 函数.pptx    第7章 类.pptx    第8章 标准库.pptx    第9章 异常.pptx    第10章 文件处理.pptx    第11章 图形用户界面.pptx    第12章 数据库操作.pptx    第13章 线程与进程.pptx    第14章 测试及打包.pptx    第15章 Web应用入门.pptx    第16章 商业级别的技术框架.pptx    第17章 大数据应用入门.pptx    第18章 AI应用入门.pptx视频学时共8小时教学视频目录:       0_前导课_与入门者交流学习编程心得.mp4    1_1第一节课概述.mp4    1_2第二节课_什么是Python语言.mp4    1_3节第三节课安装Python.mp4    1_4第四节Python代码编辑工具.mp4    1_5第五课第一个程序.mp4    1_5第六课第一个程序_出错调试.mp4    1_6第七节课良好的编程约定.mp4    2_1第一节变量.mp4    2_2第三节课字符基本操作.mp4    2_2第二节课字符串基本命名.mp4    2_3第四节课数字的运算一.mp4    2_3第四节课数字的运算二.mp4    2_4第六节课数据类型转换.mp4    3_1 第一节课if条件分支.mp4    3_1 第二节课if条件分支elif.mp4    3_2第三节课while循环.mp4    3_2第四节课while循环嵌套.mp4    3_3第五节for循环.mp4    3_4第七节课循环控制语句continue.mp4    3_4第六节课循环控制语句break.mp4    3_5第八节复杂条件及处理.mp4    4_1第一节课接触列.mp4    4_1第三节课列元素修改删除.mp4    4_1第二节课列元素增加查找.mp4    4_1第四节课列元素合并排序.mp4    4_2第五节课列冒泡排序算法.mp4    4_3第六节课元组.mp4    5_1第一节课接触字典.mp4    5_1第三节课字典的遍历.mp4    5_1第二节课字典的建立读取修改删除.mp4    5_2第四节课字典的嵌套.mp4    5_3第五节课基于字典的算法.mp4    6_1 第一节课函数基本知识.mp4    6_2 第二节课自定义函数第一步.mp4    6_3 第三节课自定义函数第二步.位置参数.mp4    6_3 第四节课自定义函数第二步默认值和不定长参数.mp4    7_1第一节初识类.mp4    7_2第二节类属性使用.mp4    7_3第三节类改造问题.mp4    7_4第四节类私有.mp4    7_5第六节类回顾.mp4    7_5第四节把类放到模块中.mp4    7_6第七节类总结.mp4    8_1第一节课Python标准库知识.mp4    8_2第二节课datetime模块.mp4    8_3第三节课math模块.mp4    8_4第四节课random模块.mp4    8_5第五节课os模块.mp4    8_6第六节课sys模块..mp4    8_7第七节课time模块.mp4    8_8第九节课再论模块_包.mp4    8_8第八节课t再论模块.mp4    8_9第十节窥探标准库源码.mp4    9_1第一节课程序中的问题.mp4    9_2第二节课捕捉异常.mp4    9_3第三节课抛出异常.mp4    10_1第一节课文本文件.mp4    10_2第二节JSON格式文件.mp4    10_3第三节XML格式文件.mp4    11_1第一节课初识图形用户界面.mp4    11_2第二节tkinter开发包.mp4    11_3第三节tkinter下的基本组件.mp4    11_4第四节tkinter的ttk子模块下组件.mp4    11_5第五节tkinter的tix子模块下组件.mp4    11_6第六节scrolledtext子模块下组件.mp4    11_7第七节拖拽组件.mp4    11_8第八节编译成可执行文件.mp4    12_1第一节课数据库使用概述.mp4    12_2第二节课关系型数据库.mp4    12_3第三节课NoSQL数据库.mp4    13_1第一节课接触多任务技术.mp4    13_2第二节课第一个多线程抢火车票.mp4    13_3第三节课线程同步.mp4    13_4第四节课线程队列模块.mp4    13_5第五节课并发进程模块.mp4    13_6第六节课其他同步方法.mp4    14_1代码测试.mp4    14_2第二节课代码打包.mp4    15_1第一节课Web基础知识.mp4    15_2第二节课Web服务器.mp4    15_3第三节课WSGI服务器接口.mp4    15_4第四节课Web应用程序开发.mp4    16 _1初识Web应用框架.mp4    16 _2第二节课webpy框架.mp4    16 _3第三节课Django框架.mp4    17_1第一节课什么是大数据.mp4    17_2第二节课一个完整的网络爬虫.mp4    17_2节一个完整的网络爬虫.mp4    17_3第三节课Python_Spark.mp4    18_1节什么是人工智能.mp4    18_2PythonAI编程库介绍.mp4    18_3第三节课Numpy应用示例.mp4    18_4第四节课三酷猫的梦.mp4    numpy专题讲座一安装及一维数组.mp4    numpy专题讲座三e底数组指数计算.mp4    numpy专题讲座二二维数组及维度调整.mp4    numpy专题讲座五把不同集合转为数组.mp4    numpy专题讲座六访问数组.mp4    numpy专题讲座四空数组0或1数组的建.mp4  

22,210

社区成员

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

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