1

我只是使用 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_priceand od.size,它会接受p.item_name. 我想知道为什么,因为 p.item_namep.item_price在同一张桌子上。

4

2 回答 2

2

您需要在分组中提及所有列,而不是聚合函数:

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 等一样

(并更改为显式书面连接)

于 2017-07-10T07:05:36.823 回答
0

你可以尝试这样的事情。当您使用 GROUP 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
于 2017-07-10T07:06:58.367 回答