dtq81142 2014-12-05 22:46
浏览 58
已采纳

我的sql select语句结果不正确

I have small mysql table with:

id name status class desk date username

It has around 300 records.

When selecting data to get the latest record by date (formatted), along with username, I use this statement:

SELECT DATE_FORMAT(MAX(date), '%d-%m-%Y at %H:%i'), username as LatestRecord from myTable

It returns the correct record for dateTime but gives different username which is not in the same selected row. It seems like it doesn't select the username for the same resulted lastest date/time.

Any idea why this happens?

  • 写回答

1条回答 默认 最新

  • dongtong1226 2014-12-05 22:51
    关注

    If you want the latest record then you should add an ORDER BY and a LIMIT clausule's to your query:

    SELECT DATE_FORMAT(date, '%d-%m-%Y at %H:%i') as LatestDate, username as LatestRecord 
    FROM myTable
    ORDER BY date DESC
    LIMIT 1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度