错误1055(42000):select列表的表达式#1不在group by子句中,并且包含未聚合的列

6qfn3psc  于 2021-06-19  发布在  Mysql
关注(0)|答案(2)|浏览(443)

我最近将mysql从5.1迁移到5.7,因为有一个特定的查询出现错误。以下是查询:

select count(*) COUNT_PRODUCTS from 
 (select d.ID, d.EAN, d.NAME, d.SAP_CATEGORY_ID, d.SAP_VENDOR_ID, d.RELEASE_DATE,
 d.REMOVED_DATE, d.IS_DRAFT, d.DATA_STATE, d.gc, ATTRIBUTE_CODE AS ATTRIB_NAME, ATTRIBUTE_VALUE AS APP_STATUS from 
 (select b.ID, b.EAN, b.NAME,  b.SAP_CATEGORY_ID, b.SAP_VENDOR_ID, b.RELEASE_DATE, 
 b.REMOVED_DATE, b.IS_DRAFT, b.DATA_STATE,  group_concat(pl.LIFECYCLE_VALUE) gc from 
 (select a.ID, a.EAN, a.NAME,  a.SAP_CATEGORY_ID, a.SAP_VENDOR_ID, a.RELEASE_DATE, 
 a.REMOVED_DATE, a.IS_DRAFT, a.DATA_STATE  from 
 (select p.ID, p.EAN, p.NAME, p.SAP_CATEGORY_ID, p.SAP_VENDOR_ID, p.RELEASE_DATE, 
 p.REMOVED_DATE, p.IS_DRAFT,  p.DATA_STATE from PRODUCTS as p   order by p.EAN, p.IS_DRAFT) as a group by a.EAN )
 as b  left join PRODUCT_LIFECYCLE_STATES pl on pl.PRODUCT_ID = b.ID  group by b.ID ) as d left join PRODUCT_ATTRIBUTE_VALUES
 AS PRODAV ON ID=PRODAV.PRODUCT_ID AND PRODAV.ATTRIBUTE_VALUE_ID IN
 (select ID from ATTRIBUTE_VALUES where ATTRIBUTE_CODE='APPROVED_ATTRIBUTES') 
 left join ATTRIBUTE_VALUES AS ATVALS ON PRODAV.ATTRIBUTE_VALUE_ID = ATVALS.ID AND ATVALS.ATTRIBUTE_CODE ='APPROVED_ATTRIBUTES'
 ORDER BY d.SAP_CATEGORY_ID, d.NAME ) f

这是我收到的错误:

ERROR 1055 (42000): 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\u模式,但是在更改查询之后,它不会返回任何输出并继续加载。

b09cbbtk

b09cbbtk1#

您只需要禁用\u full \u group by

SET sql_mode = (SELECT REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY', ''));
lrpiutwd

lrpiutwd2#

要解决此错误,请连接到mysql服务器:

mysql -u root -p

执行显示sql\U模式值的查询:

show variables like 'sql_mode';

在我看来:

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

复制具有这些值的字符串,删除“only\u full\u group\u by”并执行查询:

SET sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

通过此请求,我们禁用了“only\ full\ group\ by”,之后将不会显示错误。
然后我们退出mysql:

exit

为了以后不再出现这个错误,我们需要打开mysql配置文件。例如,在文本编辑器nano中(ctrl+x表示退出,y/n表示保存或取消更改):

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

我在文件中没有sql\u mode=,因此在文件末尾插入了带有先前复制的值的行,只从中删除了\u full\u group\u by,在我的例子中,发生了以下情况:

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

重新启动mysql以应用更改:

sudo service mysql restart

应该这样做;)。

相关问题