一对多关联查询的sql写法,请大家帮忙,谢谢!

jane_zhao 2010-06-22 03:29:33
我现在有两个表如下:
A表:
id name typeID
A1 材料 a
A2 项目 b
B表:
key(主键) typeid typeName
T1 a 1a
T2 a 2a
T3 b 1b
T4 b 2b
T5 b 3b
T6 c 1c
我需要查询出如下的数据:
id name typeID typeName
A1 材料 a 1a
A2 项目 b 1b
即:关联B表查询出主键最小的那笔对应的typeName即可,通过A表的typeID和B表的typeid关联。
请大家帮忙,在线等,谢谢!
...全文
494 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
jane_zhao 2010-06-22
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 small_agile 的回复:]

SQL code
create table [A]([id] varchar(2),[name] varchar(4),[typeID] varchar(1))
insert [A]
select 'A1','材料','a' union all
select 'A2','项目','b'
if object_id('[B]') is not null drop table [B]
go
crea……
[/Quote]
对不起了,你的方法也是ok的,但是我已经结贴了你才回答的。
small_agile 2010-06-22
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 small_agile 的回复:]
SQL code
create table [A]([id] varchar(2),[name] varchar(4),[typeID] varchar(1))
insert [A]
select 'A1','材料','a' union all
select 'A2','项目','b'
if object_id('[B]') is not null drop table [B]
go
……
[/Quote]
分又快花完了,進來賺點!!!
small_agile 2010-06-22
  • 打赏
  • 举报
回复
create table [A]([id] varchar(2),[name] varchar(4),[typeID] varchar(1))
insert [A]
select 'A1','材料','a' union all
select 'A2','项目','b'
if object_id('[B]') is not null drop table [B]
go
create table [B]([key] varchar(2),[typeid] varchar(1),[typeName] varchar(2))
insert [B]
select 'T1','a','1a' union all
select 'T2','a','2a' union all
select 'T3','b','1b' union all
select 'T4','b','2b' union all
select 'T5','b','3b' union all
select 'T6','c','1c'
select * from b

select a.*,(select top 1(typename) from b where b.typeid = a.typeid order by typeid asc) from a
yjwcwrkks 2010-06-22
  • 打赏
  • 举报
回复
学习。。
jane_zhao 2010-06-22
  • 打赏
  • 举报
回复
查询出来了,谢谢你,我搞错了,
not exists(select 1 from b t where t.typeid=b.typeid and t.[key]<b.[key])
我写成了
not exists(select 1 from b,t where t.typeid=b.typeid and t.[key]<b.[key])
.我给分了,谢谢!
jane_zhao 2010-06-22
  • 打赏
  • 举报
回复
请问:select a.*,b.typeName
from a
left join b on a.typeid=b.typeid
and not exists(select 1 from b t where t.typeid=b.typeid and t.[key]<b.[key])
中not exists语句的b和t都是B表吧?
jane_zhao 2010-06-22
  • 打赏
  • 举报
回复
我这儿是仿照表数据举了个例子,但是数据类型是一样的。
百年树人 2010-06-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 jane_zhao 的回复:]
引用 1 楼 josy 的回复:
SQL code
select a.*,b.typeName
from a
left join b on a.typeid=b.typeid
and not exists(select 1 from b t where t.typeid=b.typeid and t.[key]>b.[key])

谢谢你,但是用的方法,查询出的typeName栏……
[/Quote]

你实际的数据和你给的数据不一致?
jane_zhao 2010-06-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 josy 的回复:]

SQL code
select a.*,b.typeName
from a
left join b on a.typeid=b.typeid
and not exists(select 1 from b t where t.typeid=b.typeid and t.[key]>b.[key])
[/Quote]
谢谢你,但是用的方法,查询出的typeName栏位全为null值。
百年树人 2010-06-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 josy 的回复:]
SQL code
select a.*,b.typeName
from a
left join b on a.typeid=b.typeid
and not exists(select 1 from b t where t.typeid=b.typeid and t.[key]>b.[key])
[/Quote]
貌似反了
---测试数据---
if object_id('[A]') is not null drop table [A]
go
create table [A]([id] varchar(2),[name] varchar(4),[typeID] varchar(1))
insert [A]
select 'A1','材料','a' union all
select 'A2','项目','b'
if object_id('[B]') is not null drop table [B]
go
create table [B]([key] varchar(2),[typeid] varchar(1),[typeName] varchar(2))
insert [B]
select 'T1','a','1a' union all
select 'T2','a','2a' union all
select 'T3','b','1b' union all
select 'T4','b','2b' union all
select 'T5','b','3b' union all
select 'T6','c','1c'

---查询---
select a.*,b.typeName
from a
left join b on a.typeid=b.typeid
and not exists(select 1 from b t where t.typeid=b.typeid and t.[key]<b.[key])

---结果---
id name typeID typeName
---- ---- ------ --------
A1 材料 a 1a
A2 项目 b 1b

(2 行受影响)
百年树人 2010-06-22
  • 打赏
  • 举报
回复
select a.*,b.typeName
from a
left join b on a.typeid=b.typeid
and not exists(select 1 from b t where t.typeid=b.typeid and t.[key]>b.[key])

22,207

社区成员

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

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