数据库多表连接查询中使用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了~

posted @ 2019-10-23 10:06  Java_lively  阅读(1552)  评论(0编辑  收藏  举报