SQL error: 'database.table.field isn't in GROUP BY

↘锁芯ラ 提交于 2019-12-25 04:47:25

问题


I am getting this when I am installing a 3rd party software. I've read the related topics and all of them suggested to set sql_mode to an empty string (SET sql_mode = '') but that solution didn't work for me. Still getting the same error. Do I need to set this in my config file or something?

Here's some useful info;

Server version: 5.6.15-56-log Percona Server (GPL), Release rel63.0, Revision 519

mysql> SHOW VARIABLES LIKE 'sql_mode';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sql_mode      |       |
+---------------+-------+
1 row in set (0.00 sec)

And the failing query with error message is;

mySQL query error: SELECT MAX(e.entry_id) as max, MAX(e.entry_date) as maxdate,b.blog_id, b.blog_name, b.blog_seo_name FROM blog_entries e  LEFT JOIN blog_blogs b ON ( b.blog_id=e.blog_id )   WHERE e.entry_status='published' AND e.entry_banish=0 AND e.entry_featured=0 AND b.blog_view_level='public' AND b.blog_disabled=0 GROUP BY e.blog_id, b.blog_name, b.blog_seo_name ORDER BY maxdate DESC LIMIT 0,50

SQL error: 'db_name_ipb.b.blog_id' isn't in GROUP BY
SQL error code: 1055

Any idea ?

UPDATE

Unfortunately the codes are encrypted, so there is no way to try your suggestions. Also -as specified above- changing the SQL mode didn't solve my problem. I'll continue to investigate and update the question in case a development.


回答1:


You are selecting b.blog_id and grouping by e.blog_id. That is the problem.

SELECT MAX(e.entry_id) as max, MAX(e.entry_date) as maxdate,b.blog_id, b.blog_name, b.blog_seo_name 
FROM blog_entries e  LEFT JOIN 
     blog_blogs b ON ( b.blog_id=e.blog_id )   
WHERE e.entry_status='published' AND e.entry_banish=0 AND e.entry_featured=0 
     AND b.blog_view_level='public' AND b.blog_disabled=0 
GROUP BY b.blog_id, b.blog_name, b.blog_seo_name 
ORDER BY maxdate DESC LIMIT 0,50



回答2:


This is your query:

SELECT MAX(e.entry_id) as max, MAX(e.entry_date) as maxdate, b.blog_id, b.blog_name, b.blog_seo_name
FROM blog_entries e LEFT JOIN
     blog_blogs b
     ON b.blog_id = e.blog_id
WHERE e.entry_status = 'published' AND e.entry_banish = 0 AND
      e.entry_featured = 0 AND b.blog_view_level='public' AND
      b.blog_disabled=0
GROUP BY e.blog_id, b.blog_name, b.blog_seo_name
ORDER BY maxdate DESC
LIMIT 0,50;

You have e.blog_id in the group by but b.blog_id in the select. You should choose one or the other.

As a note: the left join can just be an inner join. The conditions in the where on the b table are turning it into an inner join anyway.

Note: MySQL usually does not enforce this. Your environment must have the ONLY_FULL_GROUP_BY mode set, so it is quite suspicious that your SQL mode does not contain anything. More information on setting SQL modes is here.



来源:https://stackoverflow.com/questions/27878604/sql-error-database-table-field-isnt-in-group-by

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!