万般无奈下,请教一条sql语句

zl13 2004-08-31 01:31:13
table1:
fd_pid(序号)
fd_uid(人员编号)
fd_date(建立日期)

table2
fd_rid(回复编号)
fd_pid(序号)
fd_uid(回复人员编号)
fd_rdate(回复日期)

table3
fd_uid(人员编号)
fd_uname(人员名称)

不论table2是否有资料都需将table1的数据库全部显示出来

格式如下
人员名称 建立日期 回复数 最后回复日期 最后回复人员名称
xxxx xxxx xxxx xxxxxx xxxxxxxxxxx

研究了半天了,万般无奈求助,望大家帮忙
...全文
236 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
saucer 2004-08-31
  • 打赏
  • 举报
回复
分开看,下面这段SQL返回一个关于你 table2的信息:最后一个回复的序号,人员名
称,与回复日期

select t2.fd_pid, t3.fd_uname, t2.fd_rdate from table2 t2
inner join table3 t3
on t2.fd_uid = t3.fd_uid
where t2.fd_rdate = (select max(fd_rdate) from table2 where fd_pid = t2.fd_pid)

这是跟当前帖子序号有关的回复数量
(select count(*) from table2 where fd_pid = t1.fd_pid) as '回复数'

下面是一个普通的外连接

select t3.fd_uname as '人员名称',
t1.fd_date as '建立日期',
(select count(*) from table2 where fd_pid = t1.fd_pid) as '回复数
',
t4.fd_rdate as '最后回复日期'
t4.fd_uname as '最后回复人员名称'
from table1 t1
inner join table3 t3 on t1.fd_uid = t3.fd_uid
left outer join (...) t4
on t1.fd_pid = t4.fd_pid
zl13 2004-08-31
  • 打赏
  • 举报
回复
谢谢大家,昨天晚上我已经恶补了,补到晚上4点多...
chang1216 2004-08-31
  • 打赏
  • 举报
回复
思归大哥 驾到 无话可说
morality 2004-08-31
  • 打赏
  • 举报
回复
照着思归老大的方法做就OK了!
看来你应该好好看看SQL基础了,这是最最基础的了!
祝你好运
luckweb 2004-08-31
  • 打赏
  • 举报
回复
这是基本功,要好好学
Eddie005 2004-08-31
  • 打赏
  • 举报
回复
思归老大出马,没话说了~
swang 2004-08-31
  • 打赏
  • 举报
回复
saucer(思归/MVP) 的方法是可以的。
fangbuge 2004-08-31
  • 打赏
  • 举报
回复
什么啊?
windinwing(潇笑),也不合题。
cuiwb770111 2004-08-31
  • 打赏
  • 举报
回复
windinwing(潇笑) 的方法比较简单实用,赞同!
fangbuge 2004-08-31
  • 打赏
  • 举报
回复
saucer(思归/MVP) 真强
windinwing 2004-08-31
  • 打赏
  • 举报
回复
select ....... from table1 as t1 left join table2 as t2 on t1.fp_pid=t2.fp_pid

left join :显示符合条件的数据行,以及左边不符合条件的数据行
saucer 2004-08-31
  • 打赏
  • 举报
回复
sorry, it needs an extra ",":

t4.fd_rdate as '最后回复日期',
t4.fd_uname as '最后回复人员名称'
saucer 2004-08-31
  • 打赏
  • 举报
回复
maybe there is a simpler solution, but try


select t3.fd_uname as '人员名称',
t1.fd_date as '建立日期',
(select count(*) from table2 where fd_pid = t1.fd_pid) as '回复数',
t4.fd_rdate as '最后回复日期'
t4.fd_uname as '最后回复人员名称'
from table1 t1
inner join table3 t3 on t1.fd_uid = t3.fd_uid
left outer join
(select t2.fd_pid, t3.fd_uname, t2.fd_rdate from table2 t2
inner join table3 t3
on t2.fd_uid = t3.fd_uid
where t2.fd_rdate = (select max(fd_rdate) from table2 where fd_pid = t2.fd_pid)) t4
on t1.fd_pid = t4.fd_pid
enterzhuang 2004-08-31
  • 打赏
  • 举报
回复
用LEFT JOIN

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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