[英]MySQL error “Expression #1 of ORDER BY clause is not in GROUP BY clause”
[英]New version of php/mysql got an aggregate error in GROUP BY clause
我的代碼出現錯誤,說“在選擇列表中無效,因為它不包含/聚集在group by子句中”。 我在“ MS SQL Server”中似乎很熟悉此錯誤。 我以前在MySQL中沒有遇到此錯誤。 當我將Php版本升級到7時出現此錯誤。以前的所有以前基於Web的程序都受到了影響。
我試圖在我的“ group by”子句中添加更多列,該錯誤消失了,但是輸出不是我所期望的。 下面的代碼是我的舊代碼。
SELECT SUM(s.pscore) as towtal, s.pscore AS totalScore, s.cri_id,
c.can_id, c.canid,c.can_name FROM score s INNER JOIN candidate c ON
s.can_id = c.can_id WHERE cat_id=1 AND s.cri_id = '".$rows['cri_id']."'
AND c.can_sex = 'Female' AND c.can_id='".$kert[$i]."'
GROUP BY s.can_id ORDER BY s.can_id ASC LIMIT 5
當我添加GROUP BY s.can_id, s.pscore
,不會有任何錯誤,但是輸出不是我所期望的
轉到數據庫並在sql中運行此命令
set GLOBAL sql_mode='';
SET sql_mode=(SELECT REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY', ''));
有效查詢的示例:
SELECT SUM(s.pscore) as towtal
, s.pscore AS totalScore
, s.cri_id
, c.can_id
, c.canid
, c.can_name
FROM score s
JOIN candidate c
ON s.can_id = c.can_id
WHERE cat_id = 1
AND s.cri_id = '".$rows['cri_id']."'
AND c.can_sex = 'Female'
AND c.can_id = '".$kert[$i]."'
GROUP
BY s.pscore
, s.cri_id
, c.can_id
, c.canid
, c.can_name
ORDER
BY s.can_id ASC
LIMIT 5;
請注意,此查詢容易被注入
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.