dpp80346 2015-07-15 09:05
浏览 37
已采纳

MySQL按部件号排序日期

Essentially I want these parts (below) grouped then the groups place in order of time, starting from the latest time being at the top of the list.

ID  Parts       Time
1   SMH_2010    08:59:18    
2   JJK_0101    08:59:26
3   FTD_0002    08:59:24    
4   JJK_0102    08:59:27    
5   FTD_0001    08:59:22    
6   SMH_2010    08:59:20    
7   FTD_0003    08:59:25    

So, the results would look like:

ID    Parts     Time
1   JJK_0101    08:59:26    
2   JJK_0102    08:59:27

3   FTD_0001    08:59:22    
4   FTD_0002    08:59:24    
5   FTD_0003    08:59:25

6   SMH_2010    08:59:20
7   SMH_2010    08:59:18

Please, I would be grateful for any help.

  • 写回答

2条回答 默认 最新

  • doudaochu1699 2017-10-30 15:44
    关注

    I finally found a solution to this. Not my ideal solution but, never the less it works.

    I added another field called max_date which by default is ‘now()’ as every new part is inserted.

    I create a prefix from the current part being inserted, something like “SMH_” as a variable called $prefix = “SMH_”;

    I have another query that directly follows the insert, which updates the max_date again, by ‘now()’ where the prefix is like $prefix.

    UPDATE parts SET max_date = now() WHERE prefix LIKE '%$prefix%'

    To display the results I use something along the line of :

    SELECT * FROM parts ORDER BY parts.max_date DESC, parts.part ASC

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?