SELECT 列表不在 GROUP BY 子句中 ERROR MySQL
SELECT list is not in GROUP BY clause ERROR MySQL
我只是使用 MySQL 命令的初学者。我搜索了这个错误,但他们的问题与我的不一样。我很难理解 SELECT list is not in GROUP BY clause and contains nonaggregated column 'japorms.p.item_price' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
中的错误
我也搜索了functionally dependent
的意思,还是没看懂。
这是我的查询:
SELECT
sum(od.item_qty) as item_qty,
p.item_name as item_name,
p.item_price as item_price,
od.size as size
from order_details as od, productmaster as p
where p.id = od.item_id
group by p.item_name
如果我从查询中删除 p.item_price
和 od.size
,它会接受 p.item_name
。我想知道为什么,因为 p.item_name
和 p.item_price
在同一个 table.
您需要在分组依据中提及所有列,而不是聚合函数:
SELECT
SUM(od.item_qty) AS item_qty,
p.item_name AS item_name,
p.item_price AS item_price,
od.size AS size
FROM order_details AS od,
JOIN productmaster AS p ON p.id = od.item_id
GROUP BY p.item_name, p.item_price, od.size
在这种情况下,SUM() 就是这样一个函数,MAX、MIN、COUNT 等也是如此
(并更改为显式书面连接)
你可以尝试这样的事情。当您使用 GROUP BY 时,您应该指定哪些列是 "grouped by" 以及哪些列是使用函数聚合的(例如 SUM、AVG、MIN、MAX、COUNT、...)
SELECT
p.item_name as item_name,
AVG(p.item_price) as item_price,
od.size as size,
SUM(od.item_qty) as item_qty
from order_details as od
INNER JOIN productmaster as p ON p.id=od.item_id
group by p.item_name, od.size
我只是使用 MySQL 命令的初学者。我搜索了这个错误,但他们的问题与我的不一样。我很难理解 SELECT list is not in GROUP BY clause and contains nonaggregated column 'japorms.p.item_price' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
我也搜索了functionally dependent
的意思,还是没看懂。
这是我的查询:
SELECT
sum(od.item_qty) as item_qty,
p.item_name as item_name,
p.item_price as item_price,
od.size as size
from order_details as od, productmaster as p
where p.id = od.item_id
group by p.item_name
如果我从查询中删除 p.item_price
和 od.size
,它会接受 p.item_name
。我想知道为什么,因为 p.item_name
和 p.item_price
在同一个 table.
您需要在分组依据中提及所有列,而不是聚合函数:
SELECT
SUM(od.item_qty) AS item_qty,
p.item_name AS item_name,
p.item_price AS item_price,
od.size AS size
FROM order_details AS od,
JOIN productmaster AS p ON p.id = od.item_id
GROUP BY p.item_name, p.item_price, od.size
在这种情况下,SUM() 就是这样一个函数,MAX、MIN、COUNT 等也是如此
(并更改为显式书面连接)
你可以尝试这样的事情。当您使用 GROUP BY 时,您应该指定哪些列是 "grouped by" 以及哪些列是使用函数聚合的(例如 SUM、AVG、MIN、MAX、COUNT、...)
SELECT
p.item_name as item_name,
AVG(p.item_price) as item_price,
od.size as size,
SUM(od.item_qty) as item_qty
from order_details as od
INNER JOIN productmaster as p ON p.id=od.item_id
group by p.item_name, od.size