请教:这个SQL语句该怎样写

hiaming 2003-08-21 05:54:07
请教:这个SQL语句该怎样写



我有两个一对多的关系表,主表 Project_Name 和 从表 Project_Books

Project_Name的数据

c_ID c_Name c_Client c_Remark Is_Del
1 XXXX软件需求分析说明书 劳动局 0
2 XXXX软件需求分析说明书 劳动局 0
......


Project_Books
主键 Project_Name.c_ID 内容
p_ID c_ID remark
1 1 内容
2 1 内容
3 1 内容
4 1 内容
5 1 内容
6 1 内容
7 1 内容
5 2 内容
6 2 内容
7 2 内容
8 2 内容
9 2 内容
10 2 内容
......

我的目的是,用一个SQL语句,选出所有方案书的资料及最新一个版本的内容
即Project_Name中的所有记录和每条记录对应的Project_Books表中取最新的一条记录。
而且我也不能确定其余条件
但我使用内联查询时,会有很多多余的行。请问这个SQL语句该如何写?
...全文
31 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hiaming 2003-08-29
  • 打赏
  • 举报
回复
搞定,谢谢大力
happydreamer 2003-08-22
  • 打赏
  • 举报
回复
select a.*, (select remark from Project_Books b where p_ID=(select max(p_ID)
from Project_Books where c_ID=a.c_ID) and b.c_ID=a.c_ID)
from Project_Name a
happydreamer 2003-08-22
  • 打赏
  • 举报
回复
select a.*, (select remark from Project_Books b where p_ID=(select max(p_ID)
from Project_Books where c_ID=a.c_ID) and b.c_ID=a.c_ID)
from Project_Name a
hiaming 2003-08-21
  • 打赏
  • 举报
回复
谢谢,我这就下线去试试
pengdali 2003-08-21
  • 打赏
  • 举报
回复
select * from Project_Name a,
(select * from Project_Books tem where p_id=(select max(p_id) from Project_Books where c_id=tem.c_id)) b
where a.c_id=b.c_id

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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