急,您请进。。。

chlxie 2003-10-20 05:24:23
table1
desc c_no name
wer abc sss
wee abe iii
rwr abw qqq
wef abq wpe

table2
c_no c_date result
abc 2002-10-1 ok
abc 2002-09-12 doing1
abc 2002-09-20 doing2
abe...
abe...
abw...
两表连接通过c_no字段
现在希望select出表2中c_date是最近日期的那条记录中table2中的result字段等

select出如下结果:
c_no name c_date result
abc sss 2002-10-1 ok
abe...
abw...
...全文
100 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
chlxie 2003-10-20
  • 打赏
  • 举报
回复
那该如何呢?
mjhnet 2003-10-20
  • 打赏
  • 举报
回复
select A.c_no,(select top 1 name from table1 where c_no=A.c_no),
A.c_date,A.result
from table2 A
where c_date=(select max(c_date) from table2 where c_no=A.c_no)

--出错是因为返回了多条记录
chlxie 2003-10-20
  • 打赏
  • 举报
回复
to:: yujohny(踏网无痕)
"select name from table1 where c_no=A.c_no"处报错
why ?
thanks
HikaruPang 2003-10-20
  • 打赏
  • 举报
回复

Select Top 1 * From Table1
Left Join Table2
On Table1.C_NO = Table2.C_No
Order By Table2.c_Date Desc
dafu71 2003-10-20
  • 打赏
  • 举报
回复
select c_no,name,A.c_date,A.result
from table1 left join
(select * from table2 as B where c_date=
(select max(c_date) from table2 where
c_no=B.c_no)) as A on A.c_no=table1.c_no
yujohny 2003-10-20
  • 打赏
  • 举报
回复
select A.c_no,(select name from table1 where c_no=A.c_no),
A.c_date,A.result
from table2 A
where c_date=(select max(c_date) from table2 where c_no=A.c_no)
spring_ok 2003-10-20
  • 打赏
  • 举报
回复
select table2.*, table1.name from table1 inner join table2 on table1.c_no = table2.c_no
where table2.c_date = (select Max(c_date) from table2)
txlicenhe 2003-10-20
  • 打赏
  • 举报
回复
try:
Select b.c_no,a.name,b.c_date,b.result
from table1 a
join table2 b on a.c_no = b.c_no
join
(Select c_no,max(c_date) as c_date from table2 group by c_no) c
on b.c_no = c.c_no and b.c_date = c.c_date

34,575

社区成员

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

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