不太熟悉数据库,特来请教一查询语句!

wrong1111 2008-08-22 10:07:44


上面是表结构...

TABLE_1 与TABEL2,TABLE_3,TABLE_4分别都是一对多的关系.
关键TABLE_1中一条记录 与上述各三表中,都只需要取最后时间插入的一条记录..
个人: 如果不用过滤TABLE2,TABLE3.TABLE4中重复记录.即可以采用外连接方式获取..
.但现在需要分别把TABLE2.TABLE3.TABLE4中的与TABLE1中对应的记录的最后一条记录取出来即可.
如.

table1
id name

1a a
2a b

table2
id 2id b1 b2 date
1 1a bb bbb 08/15
2 1a bc bc 08/16(这是最后插入)

table 3

id 3id c1 c2 date
1 2a cc ccc 08/20
2 1a cb cd 08/21

table4

id 4id d1 d2 date
1 1a dd ddd 08/21
2 1a d1 d2 08/22(这是插入)

最后结果要显示这样
t1.id,t1.name,t2.b1,t2.b2,t3.c1,t3.c2,t4.d1,t4.d2
1a a bc bc cb cd d1 d2
2a b null null cc ccc null null

哪位指点一下
...全文
149 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
regithanhu 2008-08-23
  • 打赏
  • 举报
回复
create table table1
(
id varchar(10),
name varchar(10)
)

insert into table1
select '1a','a'
union all
select '2a','b'

create table [table2]
(
id varchar(10),
[2id] varchar(10),
b1 varchar(10),
b2 varchar(10),
date smalldatetime
)

insert into table2
select '1','1a','bb','bbb','2008.08.15'
union all
select '2','1a','bc','bc','2008.08.16'

create table [table3]
(
id varchar(10),
[3id] varchar(10),
c1 varchar(10),
c2 varchar(10),
date smalldatetime
)

insert into table3
select '1','2a','cc','ccc','2008.08.20'
union all
select '2','1a','cb','cd','2008.08.21'

create table table4
(
id varchar(10),
[4id] varchar(10),
d1 varchar(10),
d2 varchar(10),
date smalldatetime
)

insert into table4
select '1','1a','dd','ddd','2008.08.21'
union all
select '2','1a','d1','d2','2008.08.22'


select t1.id,t1.name,t2.b1,t2.b2,t3.c1,t3.c2,t4.d1,t4.d2 from table1 t1
left join ( select * from table2
where date in (select max(date) from table2
group by [2id])
) t2
on t1.id=t2.[2id]
left join ( select * from table3
where date in (select max(date) from table3
group by [3id])
) t3
on t1.id=t3.[3id]
left join ( select * from table4
where date in (select max(date) from table4
group by [4id])
) t4
on t1.id=t4.[4id]

id name b1 b2 c1 c2 d1 d2
---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
1a a bc bc cb cd d1 d2
2a b NULL NULL cc ccc NULL NULL

(所影响的行数为 2 行)

个人觉得首先把table2,table3,table4查询的时候先限制好再左连接。方法不是很好,应该满足你的要求了!:)
wrong1111 2008-08-23
  • 打赏
  • 举报
回复
楼上 ybkenan
的查询条件符合我的意思....但是四个表的联接条件却不能用 = 号联接,
以表1为基表..表2,表3.表4 没有的.也要列出来...

我自己曾想过,用左外联接..但是主要是加上过滤条件之后.就会出现不是我想要的记录出来...
不知道哪位还有好的思路没有??


ybkenan 2008-08-23
  • 打赏
  • 举报
回复
select t1.id,t1.name,t2.b1,t2.b2,t3.3id
from t1,t2,t3
where t1.id = t2.2id and t1.id = t3.3id
and t2.date = (select max(date) from t2 where 2id = t1.id )
and t3.date = (select max(date) from t3 where 3id = t1.id )
樓主要類似這樣的寫。
wrong1111 2008-08-23
  • 打赏
  • 举报
回复
谢谢 楼上给的答案..

感觉楼上的有点取巧的成份...

上面的ID只是本人出的例子.如果T1 表的ID 是1234 表2 表3 表4 分别是 2345 1239 3455 这样的呢..二者之间没有必然的关联呢??.
-狙击手- 2008-08-22
  • 打赏
  • 举报
回复
select t1.id,t1.name,t2.b1,t2.b2,t3.c1,t3.c2,t4.d1,t4.d2
from table1 t1
left join table2 t2 on left(t1.id,len(t1.id)-len(t1.name)) = t2.id
left join table3 t3 on left(t1.id,len(t1.id)-len(t1.name)) = t3.id
left join table4 t4 on left(t1.id,len(t1.id)-len(t1.name)) = t4.id
comszsoft 2008-08-22
  • 打赏
  • 举报
回复
帮顶

34,838

社区成员

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

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