數據庫多表連接查詢中使用group by分組語句,Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'XXX' which is not functionally dependent on columns in GROUP BY claus


需求描述:

  要是用兩表聯合查詢,並對查詢的結果進行分組:sql如下:

    

 1 SELECT
 2 a.`id`,
 3 a.`fr_num`,
 4 b.`ent_status`
 5 FROM
 6 `table1` a
 7 LEFT JOIN `table2` b
 8 ON b.`fr_id` = a.`id`
 9 GROUP BY b.`fr_num`

  這樣的sql語句會報錯結果如下:

  Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'a.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by.

  問題處理:

  首先了解一下sql的group by的用法:

  1、select后面的查詢字段要么在group by后,要么是聚合函數

  2、如果有條件對結果進行篩選,有兩種方式:(1) 先使用where + 條件,然后使用group by (2) 先使用group by,然后使用having進行結果篩選

  解決:結合group by 的使用方法對sql進行改進就ok了~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM