select A.aid,A.name,B.status from A left join B on A.aid=B.aid where B.status=3
我直接写的,应该差不多是这样,这样的基本问题呀。。 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的时候 那你两个都查出来了 我只要最后一个数据
B表 aid status 10 3 10 3 如果有两个status=3的时候 那你两个都查出来了 我只要最后一个数据
B表 aid status 10 3 10 3 如果有两个status=3的时候 那你两个都查出来了 我只要最后一个数据
B表 aid status 10 3 10 3 如果有两个status=3的时候 那你两个都查出来了 我只要最后一个数据你就不能查出来后再程序里再做处理的。非要把所有事情都压在数据库上啊 况且按你的描述“插入了多个aid的记录,staus状态不同”哪可能存在同一个aid对应两个相通的status啊
表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
B表 aid status 10 3 10 3 如果有两个status=3的时候 那你两个都查出来了 我只要最后一个数据你就不能查出来后再程序里再做处理的。非要把所有事情都压在数据库上啊 况且按你的描述“插入了多个aid的记录,staus状态不同”哪可能存在同一个aid对应两个相通的status啊表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
事实证明来论坛也没用,自己给出答案吧 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
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
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
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
如果是join的话,可以考虑下用 B 关联A,B先group 取到最大的 记录然后和A级联 这是思路
表2 B aid,status(插入了多个aid的记录,staus状态不同) 10 1 10 2 10 3 我怎么知道哪个是最后一条记录?以什么方式进行排序?
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
select
A.aid,A.name,B.status
from
A
left join
B on A.aid=B.aid
where
B.status=3
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的时候 那你两个都查出来了 我只要最后一个数据
aid status
10 3
10 3
如果有两个status=3的时候 那你两个都查出来了 我只要最后一个数据
aid status
10 3
10 3
如果有两个status=3的时候 那你两个都查出来了 我只要最后一个数据
aid status
10 3
10 3
如果有两个status=3的时候 那你两个都查出来了 我只要最后一个数据你就不能查出来后再程序里再做处理的。非要把所有事情都压在数据库上啊
况且按你的描述“插入了多个aid的记录,staus状态不同”哪可能存在同一个aid对应两个相通的status啊
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
aid status
10 3
10 3
如果有两个status=3的时候 那你两个都查出来了 我只要最后一个数据你就不能查出来后再程序里再做处理的。非要把所有事情都压在数据库上啊
况且按你的描述“插入了多个aid的记录,staus状态不同”哪可能存在同一个aid对应两个相通的status啊表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
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
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
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
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
这是思路
aid,status(插入了多个aid的记录,staus状态不同)
10 1
10 2
10 3
我怎么知道哪个是最后一条记录?以什么方式进行排序?
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