MySql 5.7 ORDER BY 子句不在 GROUP BY 子句中且包含非聚合列
MySql 5.7 ORDER BY clause is not in GROUP BY clause and contains nonaggregated column
我想在 my.ini
中禁用 "only_full_group_by" 的情况下找出答案
这是我的查询:
SELECT
p.title,
COUNT(t.qty) AS total
FROM
payments t
LEFT JOIN products AS p
ON p.id = t.item
WHERE t.user = 1
GROUP BY t.item
ORDER BY t.created DESC;
和表格:
付款:
id item user created
============================
1 1 1 2017-01-10
2 2 1 2017-01-11
3 3 1 2017-01-12
4 4 1 2017-01-13
5 1 1 2017-01-14
产品:
id title created
==========================
1 First 2016-12-10
1 Second 2016-12-11
1 Third 2016-12-12
1 Fourth 2016-12-13
最后的结果应该看谎言:
Name Total
First 2
Second 1
Third 1
Fourth 1
但是,如果我将查询更改为 GROUP BY t.item, t.created
,错误就消失了,但我最终得到了 5 条记录,而不是 4 条,这不是我想要的。由于我根据 "item" 字段对项目进行分组,因此应该只有四个记录
对我来说,你应该按 p.title
分组查询
SELECT
p.title,
COUNT(t.qty) AS total
FROM
payments t
LEFT JOIN products AS p
ON p.id = t.item
WHERE t.user = 1
GROUP BY p.title;
或
SELECT
p.title,
COUNT(t.qty) AS total
FROM
payments t
LEFT JOIN products AS p
ON p.id = t.item
WHERE t.user = 1
GROUP BY p.title
order by p.created;
然后对于 "But if I change my query to GROUP BY t.item, t.created Error is gone" 请记住,从 mysql 5.7 开始,如果您使用 selected 列而不在 group by 中,您会出错..
如果你真的需要,你可以禁用 sql 模式的适当设置,例如
SET sql_mode = ''
https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html
最后一列按顺序对分组依据和 ONLY_FULL_GROUP_BY 参数没有影响 .. 但是如果我按顺序排列的列不在 select 中实际上没有意义
第 1 项有两个 t.created
。因此请决定要按哪个排序。例如:
ORDER BY MIN(t.created) DESC;
这是您的查询:
SELECT p.title, COUNT(t.qty) AS total
-------^
FROM payments t LEFT JOIN
products AS p
ON p.id = t.item
WHERE t.user = 1
GROUP BY t.item
---------^
ORDER BY t.created DESC;
---------^
指向的地方有问题。请注意 SELECT
和 GROUP BY
指的是不同的列。在 LEFT JOIN
中,您(几乎)总是希望通过 第一个 table 中的内容进行聚合,而不是第二个
ORDER BY
是另一个问题。您没有按此列进行聚合,因此您需要确定所需的值。我猜 MIN()
或 MAX()
:
SELECT p.title, COUNT(t.qty) AS total
FROM payments t LEFT JOIN
products AS p
ON p.id = t.item
WHERE t.user = 1
GROUP BY p.title
ORDER BY MAX(t.created) DESC;
我还要补充说 COUNT(t.qty)
是可疑的。通常 qty
指的是 "quantity" 而你想要的是总和:SUM(t.qty)
.
我想在 my.ini
中禁用 "only_full_group_by" 的情况下找出答案这是我的查询:
SELECT
p.title,
COUNT(t.qty) AS total
FROM
payments t
LEFT JOIN products AS p
ON p.id = t.item
WHERE t.user = 1
GROUP BY t.item
ORDER BY t.created DESC;
和表格:
付款:
id item user created
============================
1 1 1 2017-01-10
2 2 1 2017-01-11
3 3 1 2017-01-12
4 4 1 2017-01-13
5 1 1 2017-01-14
产品:
id title created
==========================
1 First 2016-12-10
1 Second 2016-12-11
1 Third 2016-12-12
1 Fourth 2016-12-13
最后的结果应该看谎言:
Name Total
First 2
Second 1
Third 1
Fourth 1
但是,如果我将查询更改为 GROUP BY t.item, t.created
,错误就消失了,但我最终得到了 5 条记录,而不是 4 条,这不是我想要的。由于我根据 "item" 字段对项目进行分组,因此应该只有四个记录
对我来说,你应该按 p.title
分组查询 SELECT
p.title,
COUNT(t.qty) AS total
FROM
payments t
LEFT JOIN products AS p
ON p.id = t.item
WHERE t.user = 1
GROUP BY p.title;
或
SELECT
p.title,
COUNT(t.qty) AS total
FROM
payments t
LEFT JOIN products AS p
ON p.id = t.item
WHERE t.user = 1
GROUP BY p.title
order by p.created;
然后对于 "But if I change my query to GROUP BY t.item, t.created Error is gone" 请记住,从 mysql 5.7 开始,如果您使用 selected 列而不在 group by 中,您会出错..
如果你真的需要,你可以禁用 sql 模式的适当设置,例如
SET sql_mode = ''
https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html
最后一列按顺序对分组依据和 ONLY_FULL_GROUP_BY 参数没有影响 .. 但是如果我按顺序排列的列不在 select 中实际上没有意义
第 1 项有两个 t.created
。因此请决定要按哪个排序。例如:
ORDER BY MIN(t.created) DESC;
这是您的查询:
SELECT p.title, COUNT(t.qty) AS total
-------^
FROM payments t LEFT JOIN
products AS p
ON p.id = t.item
WHERE t.user = 1
GROUP BY t.item
---------^
ORDER BY t.created DESC;
---------^
指向的地方有问题。请注意 SELECT
和 GROUP BY
指的是不同的列。在 LEFT JOIN
中,您(几乎)总是希望通过 第一个 table 中的内容进行聚合,而不是第二个
ORDER BY
是另一个问题。您没有按此列进行聚合,因此您需要确定所需的值。我猜 MIN()
或 MAX()
:
SELECT p.title, COUNT(t.qty) AS total
FROM payments t LEFT JOIN
products AS p
ON p.id = t.item
WHERE t.user = 1
GROUP BY p.title
ORDER BY MAX(t.created) DESC;
我还要补充说 COUNT(t.qty)
是可疑的。通常 qty
指的是 "quantity" 而你想要的是总和:SUM(t.qty)
.