求一个sql语句

zzxoyo 2009-07-16 05:35:37
表1新闻 table1 字段有id,name
表2评论 table2 字段有id,fid,content,time

表2的fid对应表1的id

排列新闻,按照每条新闻的最后一个评论的时间顺序,倒叙排列新闻

注:每个新闻对应很多评论,现在需要按照最后一个评论的时间来排,并且需要排列出来的新闻列表分页
...全文
29 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿_布 2009-07-18
  • 打赏
  • 举报
回复
select t1.id,t1.name from table1 as t1,table2 as t2 where t1.id=t2.fid order by t2.time desc limit 10;
MySQL数据库
阿_布 2009-07-18
  • 打赏
  • 举报
回复
你用的是什么数据库?不同数据库支持的SQL语句不同!
  • 打赏
  • 举报
回复


--ORACLE 8I 测试通过,分页可通过限制IID的范围来获得
select * from
(select rownum iid,t1.art_no,t2,buyer_uid from table1 t1,table2 t2
where t1.id=t2.fid order by t2.time desc)
where iid between 2 and 5

zzxoyo 2009-07-17
  • 打赏
  • 举报
回复
根据上面各位的,查询出来的只有一条记录,实际很多的
zzxoyo 2009-07-17
  • 打赏
  • 举报
回复
补充一点,就是有的新闻可能是没有评论的,那么此条新闻就排到后面
数据表中的time 是时间戳格式的
  • 打赏
  • 举报
回复
order by t2.time desc 就已经把没有评论的新闻放到最后了,也不晓得你的数据库是啥
dzxccsu 2009-07-16
  • 打赏
  • 举报
回复
忘记时间撮了。


select b.id,b.name,a.t from((select fid,max(id),UNIX_TIMESTAMP(time) as t from table2 group by fid )a left join (select id,name from table1)b on a.fid=b.id) order by a.t desc
dzxccsu 2009-07-16
  • 打赏
  • 举报
回复
试试这个

select b.id,b.name,a.time from((select fid,max(id),time from table2 group by fid )a left join (select id,name from table1)b on a.fid=b.id) order by b.time asc
lj14992560 2009-07-16
  • 打赏
  • 举报
回复
对了,要分页。。
select t.id,t.name,tt.id,tt.fid,tt.content,tt.time from table1 t,table2 tt where t.id = tt.fid order by UNIX_TIMESTAMP(tt.time) desc limit 0 ,10
lj14992560 2009-07-16
  • 打赏
  • 举报
回复
select t.id,t.name,tt.id,tt.fid,tt.content,tt.time from table1 t,table2 tt where t.id = tt.fid order by UNIX_TIMESTAMP(tt.time) desc
blueforyou 2009-07-16
  • 打赏
  • 举报
回复
table2中fid应该对应table1的id吧?

select t1.id,t1.name from table1 t1,(select fid,time from table2 order by time desc limit 1) t2 where t1.id=t2.fid order by t2.time desc,t1.id desc

21,891

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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