与MySql中only_full_group_by有关的错误

时间:2019-05-03 05:54:15

标签: mysql phpmyadmin

我创建了查询,该查询给出了only_full_group_by的错误。我想更改查询不是SET sql_mode = only_full_group_by

  

#1055-SELECT列表的表达式#4不在GROUP BY子句中,并且包含未聚合的列'hrdk.s.item_stock_id',该列在功能上不依赖于GROUP BY子句中的列;这与sql_mode = only_full_group_by#

不兼容

这是给我带来麻烦的查询:

SELECT `s`.`department_id`, `s`.`category_id`, `s`.`item_id`, `s`.`item_stock_id`, `s`.`tunch`, `cat`.`category_name`, `im`.`item_name`, `im`.`stock_method`, `cat`.`category_group_id`, SUM(s.grwt) AS grwt, SUM(s.ntwt) AS ntwt, sum(s.less) AS less, SUM(s.fine) AS fine
FROM `item_stock` `s`  
LEFT JOIN `item_master` `im` ON `im`.`item_id` = `s`.`item_id`  
LEFT JOIN `account` `pm` ON `pm`.`account_id` = `s`.`department_id`  
LEFT JOIN `category` `cat` ON `cat`.`category_id` = `s`.`category_id`  
WHERE (im.stock_method = 1 AND (`s`.`grwt` =0 OR `s`.`grwt` !=0)   
   OR (`im`.`stock_method` = 2 AND `s`.`grwt` != 0))  
   AND s.department_id IN(26,27,28,29,30,31,32,59)   
   AND `s`.`grwt` !=0  
   AND `s`.`department_id` = '26'   
GROUP BY `s`.`category_id`, `s`.`item_id`, if(`im`.`stock_method` = 1, `s`.`tunch`, "")  
ORDER BY `s`.`item_stock_id` DESC

让我知道是否需要更多信息。

1 个答案:

答案 0 :(得分:0)

您必须在group by

中添加所有非聚合列
SELECT s.department_id, s.category_id, s.item_id, s.item_stock_id, s.tunch, cat.category_name, im.item_name, im.stock_method, cat.category_group_id, SUM(s.grwt) AS grwt, SUM(s.ntwt) AS ntwt, sum(s.less) AS less, SUM(s.fine) AS fine 
FROM item_stock s LEFT JOIN item_master im ON im.item_id = s.item_id 
LEFT JOIN account pm ON pm.account_id = s.department_id 
LEFT JOIN category cat ON cat.category_id = s.category_id 
WHERE (im.stock_method = 1 AND (s.grwt =0 OR s.grwt !=0) OR (im.stock_method = 2 AND s.grwt != 0))AND s.department_id IN(26,27,28,29,30,31,32,59) AND s.grwt !=0 AND s.department_id = '26' 
GROUP BY s.department_id, s.category_id, s.item_id, s.item_stock_id, s.tunch, cat.category_name, im.item_name, im.stock_method, cat.category_group_id
ORDER BY s.item_stock_id DESC