求多表关联一对多且指定条件语句写法!

ppxstar 2009-05-12 04:32:45
表结构如下
表A(id,name)
表B(aid,rp1,date)
表C(aid,rp2,date)
表D(aid,rp3,date)
id与aid关联
B\C\D三表中对应A表均是一对多,既有相同aid的多条记录

要求:每表相同aid里的数据只取date最近一条记录与A表id关联
...全文
198 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
ppxstar 2009-05-14
  • 打赏
  • 举报
回复
又测试了一遍

下面两种情况
一是b,c,d表任一表中如果存在两条以上相同时间的数据,会将两条全部列出(我只要取一条,在前面加DISTINCT?)

二是如果表中只有一条数据,则根本取不出数据。

第一种情况存在,第二种情况是我看错了。

测试数据任意输入。
百年树人 2009-05-13
  • 打赏
  • 举报
回复
贴点测试数据出来看看
ppxstar 2009-05-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 josy 的回复:]
SQL codeselect
a.*,b.*,c.*
from a
left join b
on b.aid=a.id and not exists(select 1 from b t where t.aid=b.aid and t.[date]>b.[date])
left join c
on c.aid=a.id and not exists(select 1 from c t where t.aid=c.aid and t.[date]>c.[date])
left join c
on d.aid=a.id and not exists(select 1 from d t where t.aid=d.aid and t.[date]>d.[date])
[/Quote]

经试验以上代码好像有些缺陷
一是b,c,d表任一表中如果存在两条以上相同时间的数据,会将两条全部列出(我只要取一条,在前面加DISTINCT?)

二是如果表中只有一条数据,则根本取不出数据。

综上所述,该语句只有在
关联表中有两条以上时间不相同的数据才能正确输出。

请问要如何改进?
BlueSky4014 2009-05-12
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 josy 的回复:]
SQL codeselect
a.*,b.*,c.*
from a
left join b
on b.aid=a.id and not exists(select 1 from b t where t.aid=b.aid and t.[date]>b.[date])
left join c
on c.aid=a.id and not exists(select 1 from c t where t.aid=c.aid and t.[date]>c.[date])
left join c
on d.aid=a.id and not exists(select 1 from d t where t.aid=d.aid and t.[date]>d.[date])
[/Quote]

select 1 from b t where t.aid=b.aid and t.[date]>b.[date]
精炼
学习了
再问,left join 和inner join 什么时候适合使用
我写的时候都是直接join,会不会效率太低
--小F-- 2009-05-12
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ppxstar 的回复:]
not exists(select 1 from b t where t.aid=b.aid and t.[date]>b.[date])

请问这一句是什么意思?
t是别名吗?
我没有t表呀!
[/Quote]

...
水族杰纶 2009-05-12
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ppxstar 的回复:]
not exists(select 1 from b t where t.aid=b.aid and t.[date]>b.[date])

请问这一句是什么意思?
t是别名吗?
我没有t表呀!
[/Quote]
別名~~
ppxstar 2009-05-12
  • 打赏
  • 举报
回复
not exists(select 1 from b t where t.aid=b.aid and t.[date]>b.[date])

请问这一句是什么意思?
t是别名吗?
我没有t表呀!
水族杰纶 2009-05-12
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 josy 的回复:]
SQL codeselect
a.*,b.*,c.*
from a
left join b
on b.aid=a.id and not exists(select 1 from b t where t.aid=b.aid and t.[date]>b.[date])
left join c
on c.aid=a.id and not exists(select 1 from c t where t.aid=c.aid and t.[date]>c.[date])
left join c
on d.aid=a.id and not exists(select 1 from d t where t.aid=d.aid and t.[date]>d.[date])
[/Quote]

額的效率低
ppxstar 2009-05-12
  • 打赏
  • 举报
回复
这样只能挂两个表,多表怎么处理

最后得到表结构应当是
id,name,rp1,rp2,rp3
百年树人 2009-05-12
  • 打赏
  • 举报
回复
select 
a.*,b.*,c.*
from a
left join b
on b.aid=a.id and not exists(select 1 from b t where t.aid=b.aid and t.[date]>b.[date])
left join c
on c.aid=a.id and not exists(select 1 from c t where t.aid=c.aid and t.[date]>c.[date])
left join c
on d.aid=a.id and not exists(select 1 from d t where t.aid=d.aid and t.[date]>d.[date])
水族杰纶 2009-05-12
  • 打赏
  • 举报
回复
SELECT A.*,B.RP1,C.RP2,D.RP3
FROM TA A
INNER JOIN (SELECT * FROM TB T WHERE NOT EXISTS(SELECT 1 FROM TB WHERE AID=T.AID AND DATE>T.DATE))B ON A.ID=B.AID
INNER JOIN (SELECT * FROM TC T WHERE NOT EXISTS(SELECT 1 FROM TC WHERE AID=T.AID AND DATE>T.DATE))C ON A.ID=C.AID
INNER JOIN (SELECT * FROM TD T WHERE NOT EXISTS(SELECT 1 FROM TD WHERE AID=T.AID AND DATE>T.DATE))D ON A.ID=D.AID
jinjazz 2009-05-12
  • 打赏
  • 举报
回复
select * from a,
(select *,rn=row_number()over(partition by aid order by date desc) from b )b
where b.aid=a.id and b.rn=1
jinjazz 2009-05-12
  • 打赏
  • 举报
回复
select * from a,
(select * from b as bb where not exists(select 1 from b where aid=bb.aid and date>bb.date))b
where b.aid=a.id

其他类似,如果是sql2005可以用row_number

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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