MySQL中的GROUP BY打破ORDER BY列表

时间:2013-06-07 11:24:08

标签: mysql

我有这个SQL:

SELECT
    c.name AS category_name,
    a.name,
    a.path,
    a.extension,
    a.width,
    a.height,
    a.server
FROM categories c
JOIN news_categories nc ON nc.categories_id = c.id
JOIN news_attachments na ON nc.news_id = na.news_id
JOIN attachments a ON na.attachments_id = a.id
WHERE na.cover = 1
ORDER BY nc.news_id DESC

它显示了正确的结果

+---------------+------------+---------------------+-----------+-------+--------+--------+
| category_name | name       | path                | extension | width | height | server |
+---------------+------------+---------------------+-----------+-------+--------+--------+
| Игры          | c54950f283 | a6f00bcd/b25/cfaf2e | jpg       |   450 |    416 |      1 |
| Игры          | fa4249e534 | a6f00bcd/b89/cfaf2e | jpg       |   400 |    250 |      1 |
| Игры          | d7b009eeb7 | a6f00bcd/bfa/cfaf2e | jpg       |   200 |    140 |      1 |
| Игры          | 143cb7a38d | a6f00bcd/baa/cfaf2e | jpg       |   200 |    140 |      1 |
| Игры          | a70451eca7 | a6f00bcd/bc6/cfaf2e | jpg       |   200 |    140 |      1 |
| Игры          | 3ab8a0c051 | a6f00bcd/b5b/cfaf2e | jpg       |   200 |    140 |      1 |
| Игры          | 206255a07a | a6f00bcd/b98/cfaf2e | jpg       |   200 |    140 |      1 |
| Игры          | 86ef53ee1f | a6f00bcd/b93/cfaf2e | jpg       |   200 |    140 |      1 |
| Игры          | a50367f861 | a6f00bcd/b32/cfaf2e | jpg       |   200 |    140 |      1 |
| Игры          | dcfdd990c6 | a6f00bcd/b32/cfaf2e | jpg       |   200 |    140 |      1 |
| Игры          | 5a3df9f4ad | a6f00bcd/b34/cfaf2e | jpg       |   200 |    140 |      1 |
| Игры          | fb3ba0390d | a6f00bcd/b35/cfaf2e | jpg       |   200 |    140 |      1 |
| Игры          | 0e31ef118d | a6f00bcd/be5/cfaf2e | jpg       |   457 |    249 |      1 |
| Игры          | 82d41d9d62 | a6f00bcd/b97/cfaf2e | jpg       |   457 |    249 |      1 |
+---------------+------------+---------------------+-----------+-------+--------+--------+

但如果我们添加GROUP BY,则会显示结果

SELECT
    c.name AS category_name,
    a.name,
    a.path,
    a.extension,
    a.width,
    a.height,
    a.server
FROM categories c
JOIN news_categories nc ON nc.categories_id = c.id
JOIN news_attachments na ON nc.news_id = na.news_id
JOIN attachments a ON na.attachments_id = a.id
WHERE na.cover = 1
GROUP BY c.id ORDER BY nc.news_id DESC

然后结果将是

+---------------+------------+---------------------+-----------+-------+--------+--------+
| category_name | name       | path                | extension | width | height | server |
+---------------+------------+---------------------+-----------+-------+--------+--------+
| Игры          | 82d41d9d62 | a6f00bcd/b97/cfaf2e | jpg       |   457 |    249 |      1 |
+---------------+------------+---------------------+-----------+-------+--------+--------+

但它一定是这样的

+---------------+------------+---------------------+-----------+-------+--------+--------+
| category_name | name       | path                | extension | width | height | server |
+---------------+------------+---------------------+-----------+-------+--------+--------+
| Игры          | c54950f283 | a6f00bcd/b25/cfaf2e | jpg       |   450 |    416 |      1 |
+---------------+------------+---------------------+-----------+-------+--------+--------+

哪里出错了?

1 个答案:

答案 0 :(得分:1)

GROUP BY将您的记录分组到c.id。因此,每个c.id值只能获得一行。

碰巧,你的查询中有多行代表这个值,MySQL会选择并显示一行:最后一行。

对于所需的输出使用LIMIT 1

如果您想在结果集中使用多个c.id值(类别),则必须提供定位first行的平均值,通常是主键。

SELECT *
FROM Table;

+---------------+------------+----------+
| pk            | category   | data     |
+---------------+------------+----------+
| 1             | red        | a        |
| 2             | red        | b        |
| 3             | red        | c        |
| 4             | red        | d        |
| 5             | red        | e        |
| 6             | red        | f        |
| 7             | green      | g        |
| 8             | green      | h        |
| 9             | green      | i        |
| 10            | green      | j        |
| 11            | blue       | k        |
| 12            | blue       | l        |
| 13            | blue       | m        |
| 14            | orange     | n        |
+---------------+------------+----------+

SELECT *
FROM Table
GROUP BY category;

+---------------+------------+----------+
| pk            | category   | data     |
+---------------+------------+----------+
| 1             | red        | a        |
| 7             | green      | g        |
| 11            | blue       | k        |
| 14            | orange     | n        |
+---------------+------------+----------+

SELECT *
FROM Table t
WHERE pk = (SELECT MAX(pk)
            FROM Table u
            WHERE u.category = t.category);

+---------------+------------+----------+
| pk            | category   | data     |
+---------------+------------+----------+
| 6             | red        | f        |
| 10            | green      | j        |
| 13            | blue       | m        |
| 14            | orange     | n        |
+---------------+------------+----------+

Another nice way of doing it