一个比较难的Sql语句

F4Boy 2004-09-01 06:04:59
一个表有N个字段,存放人员的体检信息,
字段分别如下:
rybh(人员编号) tjxxid(体检信息id) xm(姓名) 。。。(体检信息字段)
每个人可以对应多个tjxxid,因为一个人可以体检多次
现在想查出表内所有人的最近一次体检(也就是个人tjxxid最大的记录)的体检信息,
这个SQL语句应该怎么写?

这个难就难在每个人的最大体检信息id是不同的
我写了如下的SQL,但似乎不行。
select tjxx_1, tjxx_2, ..., tjxx_n from table1 where tjxxid = (select max(tjxxid) from table1 where rybh = (select rybh from table1))
错在不能把每一个人的记录都取出来。

请高手指教。(最好用一条SQL语句写出,不要用存储过程)
...全文
82 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
playyuer 2004-09-01
  • 打赏
  • 举报
回复
yesterday2000 2004-09-01
  • 打赏
  • 举报
回复
SELECT a.*
from
tb a,(select rybh,max(tjxxid) as tjxxid from tb group by rybh) b
where a.rybh=b.rybh and a.tjxxid=b.tjxxid
zjcxc 元老 2004-09-01
  • 打赏
  • 举报
回复
--或者这样(这也是楼主自己写的那个语句的意思),但这样的效率差很多.

select *
from table1 a
where tjxxid=(select tjxxid=max(tjxxid) from table1 where rybh=a.rybh)
pbsql 2004-09-01
  • 打赏
  • 举报
回复
select t.* from t,(select rybh,max(tjxxid) tjxxid from t group by rybh) a
where t.rybh=a.rybh and t.tjxxid=a.tjxxid
zjcxc 元老 2004-09-01
  • 打赏
  • 举报
回复

select a.*
from table1 a,(select rybh,tjxxid=max(tjxxid) from table1 group by rybh)b
where a.rybh=b.rybh and a.tjxxid=b.tjxxid

34,590

社区成员

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

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