订购结果表格Mysql group by

时间:2011-04-03 17:27:12

标签: mysql sql greatest-n-per-group

好的,我有这张桌子

+-----+-----------------------------+-------------+-------------+----------+---------+
| id  | title                       | created     | updated     | category | content |
+-----+-----------------------------+---------------------+---------------------+----+
| 423 | What If I Get Sick and Die? | 2008-12-30  | 2009-03-11  | angst    | NULL    |
| 524 | Uncle Karl and the Gasoline | 2009-02-28  | NULL        | humor    | NULL    |
| 537 | Be Nice to Everybody        | 2009-03-02  | NULL        | advice   | NULL    |
| 573 | Hello Statue                | 2009-03-17  | NULL        | humor    | NULL    |
| 598 | The Size of Our Galaxy      | 2009-04-03  | NULL        | science  | NULL    |
+-----+-----------------------------+---------------------+---------------------+----+

我尝试为每个类别获取唯一行,因此我查询类似于:

SELECT id,title,category FROM `entries` group by category

结果是:

+-----+-----------------------------+----------+
| id  | title                       | category |
+-----+-----------------------------+----------+
| 537 | Be Nice to Everybody        | advice   |
| 423 | What If I Get Sick and Die? | angst    |
| 524 | Uncle Karl and the Gasoline | humor    |
| 598 | The Size of Our Galaxy      | science  |
+-----+-----------------------------+----------+

结果看起来不错,但我怎样才能获得class = humor和id = 573的行呢?

如果我查询类似:

SELECT id,title,category, max(id) FROM `entries` group by category

我得到了这样的结果:

+-----+-----------------------------+----------+---------+
| id  | title                       | category | max(id) |
+-----+-----------------------------+----------+---------+
| 537 | Be Nice to Everybody        | advice   |     537 |
| 423 | What If I Get Sick and Die? | angst    |     423 |
| 524 | Uncle Karl and the Gasoline | humor    |     573 |
| 598 | The Size of Our Galaxy      | science  |     598 |
+-----+-----------------------------+----------+---------+

显然那不是我想要的,任何帮助都会受到赞赏

我想知道mysql如何为每个组崩溃行? mysql是否只为每个组取第一行?

2 个答案:

答案 0 :(得分:2)

  • 使用加入

以及GROUP BY的查询,以获取每个类别的最大ID (我猜你确实想要每个类别的最大ID行,不是吗?)

SELECT e.id
     , e.title
     , e.created
     , e.updated
     , e.category
     , e.content
FROM entries e
JOIN
    ( SELECT max(id) AS maxid
      FROM entries
      GROUP BY category
    ) AS cat
ON e.id = cat.maxid
  • 使用IN

以及获取每个类别的最大ID的查询

SELECT id
     , title
     , created
     , updated
     , category
     , content
FROM entries
WHERE id IN
    ( SELECT max(id)
      FROM entries
      GROUP BY category
    )
  • 使用任何

和相关的子查询

SELECT e.id
     , e.title
     , e.created
     , e.updated
     , e.category
     , e.content
FROM entries e
WHERE e.id >= ANY
    ( SELECT cat.id
      FROM entries cat
      WHERE e.category = cat.category
    )
  • 使用NOT EXISTS

和相关的子查询

SELECT e.id
     , e.title
     , e.created
     , e.updated
     , e.category
     , e.content
FROM entries e
WHERE NOT EXISTS
    ( SELECT 1
      FROM entries cat
      WHERE cat.id > e.id
        AND e.category = cat.category
    )

答案 1 :(得分:-1)

我认为这就是你想要的。我没有测试过它。

select * from entries e
inner join (select max(id), category from entries group by category) ids
on e.id=ids.id