sql 分组抓取最新记录集锦

1、SELECT * FROM `bookcontent` group  by book_id
bookcontent是用来存放图书内容的一个表,其中图书内容是根据图书ID一章一章连载的,我现在想把最新每本书更新的最新章节给抓出来,使用了如下语句:SELECT * FROM `bookcontent` group  by book_id order by addtime desc,

可是这样抓出来的结果并不是我想要的,这样只能抓出最早插入的图书章节记录,怎么才能抓出最后插入的记录呢


补充下,假如数据表如下:
table: bookcontent

id title book_id addtime
1 a       1              2008-2-1
2 b        1             2008-3-1
3  c       1              2008-4-1
4  a       2             2008-2-5
5   b     2              2008-3-5
6   c      2             200-4-5
如何同时只抓出
3   c       1              2008-4-1
6   c      2             200-4-5

这两条记录


试试
1、select   *   from   bookcontent  as  aaa     where   not   exists(   
          select   *   from   bookcontent  where   book_id =aaa.book_id    and   addtime > a.addtime)

2、select * from bookcontent  where addtime in(select max(addtime) from bookcontent goup by book_id)


2、



select * from (select * from 表 order by 最大值的那个字段 desc)

temp group by 所需分组的字段






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值