当前位置: 首页 > 工具软件 > Group-Co > 使用案例 >

mysql中报错1140 - In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggreg

裴威
2023-12-01

执行sql查询语句:

SELECT
    IFNULL(
        c.card_type_name,
        '**名字'
    ) AS card_type_name,
    IFNULL(sum(a.money), 0) AS frje
FROM
    agent_profit a
LEFT JOIN card_index b ON a.card_no = b.card_no
LEFT JOIN card_type c ON b.card_type = c.card_type
WHERE
    1 = 1
AND a.cre_time LIKE '2020-09%'
AND c.card_type = 'card_**lt'

报错

[Err] 1140 - In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'cust_cx.c.card_type_name'; this is incompatible with sql_mode=only_full_group_by

[Err]1140-在没有GROUP BY的聚合查询中,选择列表的表达式1包含非聚合列“cust_cx.c.card_type_name”;这与sql“mode=only”full“GROUP”BY不兼容

在不使用group by 子句的聚合查询中,Select列表中的第一个表达式包含了非聚合的列**(改为聚会函数形成就可以正常执行了);当sql_mode为only_full_group_by时,是不能出现这种情况的。
也就是说,mysql的sql_mode是only_full_group_by的时候,在不使用group by 并且select后面出现聚集函数的话,那么所有被select的都应该是聚集函数,否则就会报错

解决:

查询mysql安装文件:whereis mysql
编辑/etc/my.cnf文件,加入如下参数,

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

 

my.cnf文件:修改前

[mysqld]
basedir=/**/**/mysql-5.7.25
datadir=/**/**/mysql-5.7.25/data
socket=/tmp/mysql.sock
port=3306



# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/**/**/mysql-5.7.25/data/mysqld.log
pid-file=/**/**/mysql-5.7.25//data/mysqld.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

修改后: 

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
 basedir = /**/**/mysql
 datadir = /**/**/data
 port = 3306
 server_id = 100
 socket = /tmp/mysql.sock

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

重启mysql

service mysqld restart

 类似资料: