免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4959 | 回复: 18
打印 上一主题 下一主题

求助一个SQL语句,高手请进 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-10-16 14:33 |只看该作者 |倒序浏览
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 ...

论坛徽章:
0
2 [报告]
发表于 2008-10-16 14:33 |只看该作者
你确认我写的不行
select cid,id,updatetime from product as a where updatetime=(select max(updatetime) from product where cid=a.cid) order by a.cid
mysql> 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 | 1991-02-21 00:00:00 |
|  5 |    2 | 2008-10-06 00:00:00 |
|  6 |    2 | 1996-10-28 00:00:00 |
|  7 |    1 | 2008-10-19 00:00:00 |
|  8 |    1 | 1990-10-18 00:00:00 |
|  9 |    2 | 2008-02-04 00:00:00 |
+----+------+---------------------+

mysql> select cid,id,updatetime from product as a where updatetime=(select max(updatetime) from product where cid=a.cid) order by a.cid;
+------+----+---------------------+
| cid  | id | updatetime          |
+------+----+---------------------+
|    1 |  7 | 2008-10-19 00:00:00 |
|    2 |  1 | 2008-10-24 00:00:00 |
+------+----+---------------------+
2 rows in set (0.00 sec)

论坛徽章:
8
综合交流区版块每周发帖之星
日期:2015-12-02 15:03:53数据库技术版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每日发帖之星
日期:2015-09-14 06:20:00金牛座
日期:2014-10-10 11:23:34CU十二周年纪念徽章
日期:2013-10-24 15:41:34酉鸡
日期:2013-10-19 10:17:1315-16赛季CBA联赛之北京
日期:2017-03-06 15:12:44
3 [报告]
发表于 2008-10-16 14:37 |只看该作者
你给的语句说结果不对
把结果拿出来

还有把你预想的结果也拿出来

[ 本帖最后由 ruochen 于 2008-10-16 14:38 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2008-10-16 15:12 |只看该作者
select cid,id,updatetime from product as a where updatetime=(select max(updatetime) from product where cid=a.cid) order by a.cid
    我只能说这种sql是找k 呵呵

论坛徽章:
0
5 [报告]
发表于 2008-10-16 16:09 |只看该作者
已解决,谢谢大家

论坛徽章:
8
综合交流区版块每周发帖之星
日期:2015-12-02 15:03:53数据库技术版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每日发帖之星
日期:2015-09-14 06:20:00金牛座
日期:2014-10-10 11:23:34CU十二周年纪念徽章
日期:2013-10-24 15:41:34酉鸡
日期:2013-10-19 10:17:1315-16赛季CBA联赛之北京
日期:2017-03-06 15:12:44
6 [报告]
发表于 2008-10-16 16:32 |只看该作者
原帖由 wleige 于 2008-10-16 16:09 发表
已解决,谢谢大家


你楼上给的语句已经搞定了??

论坛徽章:
0
7 [报告]
发表于 2008-10-16 16:40 |只看该作者
咋解决的啊,悬赏要给分啊
做人要厚道啊

论坛徽章:
0
8 [报告]
发表于 2008-10-16 16:42 |只看该作者
原帖由 gogo407 于 2008-10-16 16:40 发表
咋解决的啊,悬赏要给分啊
做人要厚道啊

你写的不行,怎么给分呀

论坛徽章:
0
9 [报告]
发表于 2008-10-16 16:46 |只看该作者
原帖由 ruochen 于 2008-10-16 16:32 发表


你楼上给的语句已经搞定了??

不是他写的,所以没有给他分,他很不满意。
是这样的,先用子查询排序,再用group by cid  order by  updatetime desc

论坛徽章:
0
10 [报告]
发表于 2008-10-16 16:57 |只看该作者
不对的话,请明示结果
给不给分不是关键的,只是想知道是不是真的有问题而已:)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP