MySQL分组,但显示所有和按日期排序

时间:2013-09-11 05:46:01

标签: php mysql sql group-by

我有以下mysql表

ID (int)        parent_id (int)        title (str)     date (int - unix timestamp)
------------------------------------------------------------------------------------
252                 2                  Title 1         1378872876  (01. sept)
262                 2                  Title 2         1378870827  (02. sept)
273                 3                  Title 3         1378870800  (04. sept)
296                 3                  Title 4         1378869614  (05. sept)
301                 2                  Title 5         1378869384  (02. sept)
324                 4                  Title 6         1378868522  (02. sept)
339                 3                  Title 7         1378865096  (03. sept)
355                 3                  Title 8         1378864830  (02. sept)
365                 4                  Title 9         1378863323  (04. sept)
366                 4                  Title 10        1378863240  (02. sept)
376                 2                  Title 11        1378859731  (01. sept)
381                 5                  Title 12        1378858380  (03. sept)
388                 3                  Title 13        1378858080  (02. sept)
392                 4                  Title 14        1378857600  (04. sept)

我想要做的是选择按unix时间戳(仅一天)排序的所有行,但也按parent_id分组。因此,SQL查询应返回以下内容:

ID (int)        parent_id (int)        title (str)     date (int - unix timestamp)
------------------------------------------------------------------------------------
296                 3                  Title 4         1378869614  (05. sept)
273                 3                  Title 3         1378870800  (04. sept)
339                 3                  Title 7         1378865096  (03. sept)
388                 3                  Title 13        1378858080  (02. sept)
355                 3                  Title 8         1378864830  (02. sept)
392                 4                  Title 14        1378857600  (04. sept)
365                 4                  Title 9         1378863323  (04. sept)
366                 4                  Title 10        1378863240  (02. sept)
324                 4                  Title 6         1378868522  (02. sept)
381                 5                  Title 12        1378858380  (03. sept)
262                 2                  Title 2         1378870827  (02. sept)
301                 2                  Title 5         1378869384  (02. sept)
252                 2                  Title 1         1378872876  (01. sept)
376                 2                  Title 11        1378859731  (01. sept)

我尝试了以下但是它并不是我想要的:

 SELECT 
         f.*, 
         FROM_UNIXTIME(f.post_date , '%d' ) AS day, 
         FROM_UNIXTIME(f.post_date , '%H' ) AS hour 
    FROM feeds f 
    WHERE f.parent_id IN (200,194,176,182,190,181,189,196,191) 
    GROUP BY f.parent_id 
    ORDER BY f.post_date DESC  
    LIMIT 0,100

这可以仅使用mysql完成,还是必须构建多个查询?

3 个答案:

答案 0 :(得分:2)

SELECT f.*
FROM feeds f
INNER JOIN (SELECT parent_id, MAX(post_date) AS Ordered GROUP BY parent_id) o
ON f.parent_id = o.parent_id
ORDER BY o.Ordered

答案 1 :(得分:0)

通过parent_id查找最新帖子,使用其时间戳作为第一列来排序,parent_id作为第二列(如果是两个相同的时间戳),最后是实际时间戳。

SELECT 
     f.*, 
     FROM_UNIXTIME(f.post_date , '%d' ) AS day, 
     FROM_UNIXTIME(f.post_date , '%H' ) AS hour 
FROM feeds f
JOIN ( SELECT parent_id, MAX( post_date ) as latest_post
          FROM feeds
          GROUP BY parent_id ) fl
ON f.parent_id = fl.parent_id
ORDER BY fl.latest_post DESC, parent_id, f.post_date DESC

答案 2 :(得分:0)

你需要告诉mysql它应该如何聚合非分组列。当您订购时,您可以通过别名日或功能来完成。即:

ORDER BY FROM_UNIXTIME(f.post_date , '%d' )

ORDER BY DAY

别名DAY可能是保留关键字,因此我会考虑更改它。