php mysql Group By to get latest record, not first record

前端 未结 5 663
醉话见心
醉话见心 2020-12-05 15:04

The Table:

(`post_id`, `forum_id`, `topic_id`, `post_time`) 
(79, 8, 4, \'2012-11-19 06:58:08\');
(80, 3, 3, \'2012-11-19 06:58:42\'),
(81, 9, 9, \'2012-11-1         


        
相关标签:
5条回答
  • 2020-12-05 15:28

    try something like

    SELECT post_id, forum_id, topic_id 
    FROM   (SELECT post_id, forum_id, topic_id
            FROM posts
            ORDER BY post_time DESC) 
    GROUP BY topic_id 
    ORDER BY topic_id desc
    LIMIT 0,5
    

    change the order by and limit as needed.

    0 讨论(0)
  • 2020-12-05 15:39

    This will also work fine for you.

    SELECT *
    FROM (
      SELECT post_id, forum_id, topic_id FROM posts
      ORDER BY post_time DESC
      LIMIT 5
    ) customeTable
    GROUP BY topic_id
    
    0 讨论(0)
  • 2020-12-05 15:44

    If you select attributes that are not used in the group clause, and are not aggregates, the result is unspecified. I.e you don't know which rows the other attributes are selected from. (The sql standard does not allow such queries, but MySQL is more relaxed).

    The query should then be written e.g. as

    SELECT post_id, forum_id, topic_id
    FROM posts p
    WHERE post_time =
      (SELECT max(post_time) FROM posts p2
       WHERE p2.topic_id = p.topic_id
       AND p2.forum_id = p.forum_id)
    GROUP BY forum_id, topic_id, post_id
    ORDER BY post_time DESC
    LIMIT 5;
    

    or

    SELECT post_id, forum_id, topic_id FROM posts
    NATURAL JOIN
    (SELECT forum_id, topic_id, max(post_time) AS post_time
     FROM posts
     GROUP BY forum_id, topic_id) p
    ORDER BY post_time
    LIMIT 5;
    
    0 讨论(0)
  • 2020-12-05 15:49

    Maybe not the best way of doing it but sometimes the function group_concat() can be userfull, it will return a string of all aggregated values sorted like you want and seperated by comma (coupled value are separated by space). I then use the function SPLIT_STRING() to cut the first id in the string.

    SELECT  
    post_id, 
    SPLIT_STRING( group_concat( forum_id, post_time ORDER BY post_time DESC ) ,' ',1 )as forum_id, 
    SPLIT_STRING( group_concat( topic_id, post_time ORDER BY post_time DESC ) ,' ',1 )as topic_id ,
    FROM posts 
    GROUP BY topic_id 
    ORDER BY post_time DESC
    LIMIT 5
    

    So the aggregated forum_id, post_time will be like this :

    81 2012-11-19 06:59:04,83 2012-11-19 16:07:46

    So you need to work with a string representation of integers and datetime couples, each couples separated by comma so I used this function to get the first INT :

    CREATE FUNCTION SPLIT_STRING(str VARCHAR(255), delim VARCHAR(12), pos INT)
    RETURNS VARCHAR(255)
    RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(str, delim, pos),
           LENGTH(SUBSTRING_INDEX(str, delim, pos-1)) + 1),
           delim, '');
    

    Note : function SPLIT_STRING(str, delim, pos) was found here : Equivalent of explode() to work with strings in MySQL

    0 讨论(0)
  • 2020-12-05 15:50

    It's not very beautiful , but it works:

    SELECT * FROM (SELECT  post_id, forum_id, topic_id FROM posts
    ORDER BY post_time DESC) as temp
    GROUP BY topic_id
    
    0 讨论(0)
提交回复
热议问题