mysql-問題解決-this is incompatible with sql_mode=only_full_group_by
技術標籤:mysql
1.問題發生
問題
1055 - Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'reporter.reporter_status.N_APP_CODE' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
字面意思
this is incompatible with sql_mode=only_full_group_by
1055 - Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'reporter.reporter_status.N_APP_CODE' which is not functionally dependent on columns in GROUP BY clause
選擇列表的表示式#2不在GROUP BY子句中,並且包含非聚合列的reporter_status。N_APP_CODE',它在功能上不依賴於GROUP BY子句中的列;
問題發生的語句
select id,count(*),an
from tb4
group by id
2.問題解決
2.1問題分析
資料來源
問題原因:id為1的分組裡面出現了兩個an(1和4)。所以出現了選擇的問題。
2.2 解決方案:
方案1.為查找出的發生碰撞的欄位加上any_value函式,於是就成了下面這種
select id,count(*),any_value(an)
from tb4
group by id
結果
方案2 設定sql_mode
2.1 獲取 @@sql_mode
select @@sql_mode
結果如下,這裡的ONLY_FULL_GROUP_BY就是要刪除的模式
ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
2.2 設定@@sql_mode(有兩種方法)
長效
SET GLOBAL sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
臨時有效 (重啟後會失效)
SET SESSION sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
方案3.設定配置檔案(在my.ini檔案中追加如下配置)
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION