The columns in the SELECT statement are not in the GROUP BY clause and contain non-aggregated columns....Incompatible with sql_mode=only_full_group_by
P粉521748211
2023-08-21 13:21:31
<p>I'm using MySQL 5.7.13 with WAMP server on my Windows PC</p>
<p>My problem is when executing this query</p>
<pre class="brush:php;toolbar:false;">SELECT *
FROM `tbl_customer_pod_uploads`
WHERE `load_id` = '78' AND
`status` = 'Active'
GROUP BY `proof_type`</pre>
<p>I always get this error</p>
<blockquote>
<p>Expression #1 in the SELECT list is not in the GROUP BY clause and contains the non-aggregate column 'returntr_prod.tbl_customer_pod_uploads.id' which has no functional dependencies on the columns in the GROUP BY clause; this is consistent with sql_mode = only_full_group_by is not compatible</p>
</blockquote>
<p>Please tell me the best solution. </p>
<p>The results I need are as follows</p>
<pre class="brush:php;toolbar:false;"> ---- --------- --------- --------- -- -------- ---------- ------------ --------------- ---- ---------- ------------ -------- -------------------- -------------------------
| id | user_id | load_id | bill_id | latitude | langitude | proof_type | document_type | file_name | is_private | status | createdon | updatedon |
---- --------- --------- --------- ---------- --------- ---------------------------------------------- ----- -------- ------------------------ ---------------- -----
| 1 | 1 | 78 | 1 | 21.1212 | 21.5454 | 1 | 1 | id_Card.docx | 0 | Active | 2017-01-27 11:30:11 | 2017-01-27 11:30:14 |
---- --------- --------- --------- ---------- --------- ---------------------------------------------- ----- -------- ------------------------ ---------------- ----- </pre>
<p><br /></p>
There is a system variable
ONLY_FULL_GROUP_BYin the MySql engine.Starting with Mysql version 5.7.5 :
ONLY_FULL_GROUP_BYSQL mode is enabled by default.Prior to version 5.7.5:
ONLY_FULL_GROUP_BYis not enabled by default.If
ONLY_FULL_GROUP_BYSQL mode is enabled (enabled by default starting with version 5.7.5), MySQL will reject query select lists,HAVINGconditions, orORDER BYThe list references non-aggregate columns that are neither named in theGROUP BYclause nor are they dependent on them.To resolve this issue, use one of the following three solutions
(1) PHPMyAdmin
Disable:
ONLY_FULL_GROUP_BYModeIf you are using phpMyAdmin, please change the
sql_modesettings according to the screenshot below.Edit
sql modeVariable and remove from valueONLY_FULL_GROUP_BYTextor
(2) SQL/Command Prompt
Disable by running the following command:
ONLY_FULL_GROUP_BYmode.or
(3) Do not use
SELECT *Do not disable
ONLY_FULL_GROUP_BYmode, insteadUse related columns in
SELECTqueries. Related columns refer to columns that appear in thegroup byclause or have aggregate functions (MAX,MIN,SUM,COUNTetc.) columnimportant
Changes made using
point(1) or point(2)are not set permanently and will be restored after every reboot.Therefore, you should set this in the configuration file (for example, in
[mysqld]section/etc/mysql/my.cnf) so that when MySQL restarts Changes still take effect after startup:Configuration file:
/etc/mysql/my.cnfVariable name:
sql_modeorsql-modeRemove the word
ONLY_FULL_GROUP_BYfrom the value and save the file.Note: If the
at the end of the filesql_modevariable is not found in the configuration file, please insert the following 2 linesThis
This problem will be solved simply by changing the SQL mode in MySQL, using the following command:
Works for me too... I used this because in my project I had a lot of similar queries so I just changed this SQL mode to only_full_group_by
Or simply include all columns specified in the SELECT statement in the GROUP BY clause. sql_mode can remain enabled.
Thanks... :-)
Update date: July 14, 2023
Changing the SQL schema is a workaround, but best practice for structured query languages would be to avoid selecting all (SELECT *...) columns and instead use aggregate functions on grouped columns like @Tim Biegeleisen https://stackoverflow.com/a/41887524/3602846
mentioned in the answer below