sqlserver一对多查询

askformore110 2014-01-16 05:57:28
表1 A
aid,name (aid唯一)
10 sel
表2 B
aid,status(插入了多个aid的记录,staus状态不同)
10 1
10 2
10 3
A关联查询B的最后一条数据,并可进行status条件查询(where B.status=3)
结果
aid name status
10 sel 3
...全文
463 19 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
wskicfuu 2014-01-19
  • 打赏
  • 举报
回复
B表必须要排序,不然不可能找到你说的B表的最后一条记录
wangyuping_2007 2014-01-18
  • 打赏
  • 举报
回复
实在是简单,看来都会
wangzhi0321 2014-01-17
  • 打赏
  • 举报
回复
create table #a(
id int,
name varchar(50)
)
insert into #a(id,name) values(10,'aa')
insert into #a(id,name) values(11,'bb')
insert into #a(id,name) values(12,'cc')
insert into #a(id,name) values(13,'dd')

create table #b(
id int,
statuss int
)
insert into #b(id,statuss) values(10,3)
insert into #b(id,statuss) values(11,4)
insert into #b(id,statuss) values(11,3)
insert into #b(id,statuss) values(10,5)

--#b表 statuss 最大值

select MAX(statuss) statuss,id  into #c from #b group by id
select a.name, c.id,c.statuss from #a a inner join #c c on
a.id=c.id
--where b.statuss=3


drop table #a
drop table #b
drop table #c
lyx266 2014-01-17
  • 打赏
  • 举报
回复

select 
   A.aid,A.name,B.status 
from 
   A 
   left join 
   (select max(bid) bid,aid,status from B group by aid,status)B on A.aid=B.aid 
where
   B.status=3
askformore1100 2014-01-17
  • 打赏
  • 举报
回复
事实证明来论坛也没用,自己给出答案吧 select a.aid,a.name,b.status from A where (select Max(bid) from B where A.aid=B.bid and.status=3) is not null
askformore110 2014-01-17
  • 打赏
  • 举报
回复
坐等高手。。
askformore110 2014-01-17
  • 打赏
  • 举报
回复
引用 7 楼 starfd 的回复:
[quote=引用 6 楼 askformore110 的回复:] [quote=引用 3 楼 pukuimin1226 的回复:] 我直接写的,应该差不多是这样,这样的基本问题呀。。 select a.aid,a.name,b.status from a join b on a.aid=b.aid where b.status=3
B表 aid status 10 3 10 3 如果有两个status=3的时候 那你两个都查出来了 我只要最后一个数据[/quote] 你就不能查出来后再程序里再做处理的。。。。非要把所有事情都压在数据库上啊 况且按你的描述“插入了多个aid的记录,staus状态不同”哪可能存在同一个aid对应两个相通的status啊[/quote] 表1 A aid,name (aid唯一) 10 sel 表2 B bid,aid,status(插入了多个aid的记录) 1 10 1 2 10 2 3 10 3 4 10 2 5 10 3 A关联查询B的最后一条数据,并可进行status条件查询(where B.status=3) 结果 aid name status 10 sel 3
askformore110 2014-01-17
  • 打赏
  • 举报
回复
表1 A aid,name (aid唯一) 10 sel 表2 B bid,aid,status(插入了多个aid的记录) 1 10 1 2 10 2 3 10 3 4 10 2 5 10 3 A关联查询B的最后一条数据,并可进行status条件查询(where B.status=3) 结果 aid name status 10 sel 3
  • 打赏
  • 举报
回复
引用 6 楼 askformore110 的回复:
[quote=引用 3 楼 pukuimin1226 的回复:] 我直接写的,应该差不多是这样,这样的基本问题呀。。 select a.aid,a.name,b.status from a join b on a.aid=b.aid where b.status=3
B表 aid status 10 3 10 3 如果有两个status=3的时候 那你两个都查出来了 我只要最后一个数据[/quote] 你就不能查出来后再程序里再做处理的。。。。非要把所有事情都压在数据库上啊 况且按你的描述“插入了多个aid的记录,staus状态不同”哪可能存在同一个aid对应两个相通的status啊
askformore110 2014-01-17
  • 打赏
  • 举报
回复
引用 3 楼 pukuimin1226 的回复:
我直接写的,应该差不多是这样,这样的基本问题呀。。 select a.aid,a.name,b.status from a join b on a.aid=b.aid where b.status=3
B表 aid status 10 3 10 3 如果有两个status=3的时候 那你两个都查出来了 我只要最后一个数据
askformore110 2014-01-17
  • 打赏
  • 举报
回复
引用 2 楼 mail_ylei 的回复:

select 
   A.aid,A.name,B.status 
from 
   A 
left join 
   B on A.aid=B.aid 
where
   B.status=3

B表 aid status 10 3 10 3 如果有两个status=3的时候 那你两个都查出来了 我只要最后一个数据
askformore110 2014-01-17
  • 打赏
  • 举报
回复
引用 1 楼 zxy397472251 的回复:

select b.aid,a.name,b.status from a inner join b on  a.aid=b.aid
where b.status=3
B表 aid status 10 3 10 3 如果有两个status=3的时候 那你两个都查出来了 我只要最后一个数据
md5e 2014-01-17
  • 打赏
  • 举报
回复
Select * From( Select aid,[name],(select top 1 [status] From B Where A.aid=B.aid Order by [status] DESC) as status From A ) Where status=3
md5e 2014-01-17
  • 打赏
  • 举报
回复
表2 B aid,status(插入了多个aid的记录,staus状态不同) 10 1 10 2 10 3 我怎么知道哪个是最后一条记录?以什么方式进行排序?
djy252 2014-01-17
  • 打赏
  • 举报
回复
如果是join的话,可以考虑下用 B 关联A,B先group 取到最大的 记录然后和A级联 这是思路
mail_ylei 2014-01-17
  • 打赏
  • 举报
回复


select 
  top 1 A.aid,A.name,B.status 
from 
   A 
left join 
   B on A.aid=B.aid 
where
   B.status=3
order by
   B.bid desc

真爱无限 2014-01-16
  • 打赏
  • 举报
回复
我直接写的,应该差不多是这样,这样的基本问题呀。。 select a.aid,a.name,b.status from a join b on a.aid=b.aid where b.status=3
mail_ylei 2014-01-16
  • 打赏
  • 举报
回复

select 
   A.aid,A.name,B.status 
from 
   A 
left join 
   B on A.aid=B.aid 
where
   B.status=3

CqCoder 2014-01-16
  • 打赏
  • 举报
回复

select b.aid,a.name,b.status from a inner join b on  a.aid=b.aid
where b.status=3

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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