我试图在不禁用 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
Error 消失了,但我最终得到了 5 条记录而不是 4 条记录,这不是我想要的。由于我根据“项目”字段对项目进行分组,因此应该只有四条记录