V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
seth19960929
V2EX  ›  问与答

无开窗函数的情况下可以实现分组后取最大的一条吗?

  •  
  •   seth19960929 · 10 天前 · 360 次点击

    MySQL 版本: 5.7

    数据如下

    id, article_id, comment_id, comment_at
    1,  1,           1,         2024-09-13 14:32:39
    2,  1,           2,         2024-09-13 14:32:39
    3,  1,           3,         2024-09-13 14:32:30
    4,  2,           4,         2024-09-13 14:32:30
    

    想要的结果

    articel_id,  last_comment_id,  last_comment_at
    1,           2,                 2024-09-13 14:32:39
    2,           4,                 2024-09-13 14:32:30
    

    • 试了子查询, id=1,2 的时间一行会导致查询重复, 然后同事给了我一个 SQL (能完成效果)
    select article_id, max(concat(comment_at, '_', comment_id )) from xxx group by article_id
    
    • 除了这样的方式, 还有别的方式吗?
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1045 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 22:22 · PVG 06:22 · LAX 15:22 · JFK 18:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.