MySQL - using GROUP BY and DESC

前端 未结 5 1715
慢半拍i
慢半拍i 2020-12-31 08:03

In my SQL query I am selecting data with GROUP BY and ORDER BY clauses. The table has the same numbers across multiple rows wi

相关标签:
5条回答
  • 2020-12-31 08:24

    work-around is to re-write the query as:

    SELECT * FROM (SELECT * FROM table ORDER BY time DESC) AS t GROUP BY numbers;
    
    0 讨论(0)
  • 2020-12-31 08:27

    According to the manual you can add desc to the group by list: Example:
    group by item1, item2 desc, item3

    with or without rollup.

    I've tried this and it works in Ubuntu version 5.5.58. The reference page is: https://dev.mysql.com/doc/refman/5.7/en/group-by-modifiers.html

    0 讨论(0)
  • 2020-12-31 08:34
    SELECT * FROM TABLE GROUP BY numbers DESC;
    

    This will give you last record from group.

    Thanks

    0 讨论(0)
  • 2020-12-31 08:35
    SELECT * 
    FROM table t
    WHERE time = (
        SELECT max(time)
        FROM table
        WHERE t.numbers = numbers
    )
    
    0 讨论(0)
  • 2020-12-31 08:35
    SELECT * FROM table
        WHERE time IN (
            SELECT MAX(time)
                FROM table
                GROUP BY numbers
        )
    
    0 讨论(0)
提交回复
热议问题