- 论坛徽章:
- 0
|
5可用积分
产品表中有很多公司的产品,要得到每个公司最新的一条记录,并将查询的记录按时间排序。
注意:最新记录是该公司的产品最新更新的记录,也就是updatetime最大的,不是id最大的
表名
product
产品ID
id
公司ID
cid
更新时间
updatetime
可以有子查询,但只要一条SQL语句
关键问题是不知道如何得到最新的一条记录
select cid,id,updatetime from product group by cid order by updatetime desc 这样是不对的。
[ 本帖最后由 wleige 于 2008-10-16 16:13 编辑 ] |
最佳答案
查看完整内容
你确认我写的不行select cid,id,updatetime from product as a where updatetime=(select max(updatetime) from product where cid=a.cid) order by a.cidmysql> select * from product;+----+------+---------------------+| id | cid | updatetime |+----+------+---------------------+| 1 | 2 | 2008-10-24 00:00:00 || 2 | 2 | 2008-10-05 00:00:00 || 3 | 1 | 2008-01-06 00:00:00 || 4 | 2 | 199 ...
|