EDIT: This has been identified as possible duplicate of question #1055 - Expression of SELECT list is not in GROUP BY clause and contains nonaggregated column this is incompatible with sql_mode=only_full_group_by
This is incorrect due to the answer being to change the global sql_mode settings. MySQL changed the ONLY_FULL_GROUP_BY default for good reason based on functional dependencies and returning arbitrary data
My question and answer give a short but accurate explaination on the error and how to circumvent it using more verbose statements.
Thanks for reading, the table is as follows:
+-----------+-------------+-----------+----------+-------+
| productID | productCode | name | quantity | price |
+-----------+-------------+-----------+----------+-------+
| 1001 | PEN | Pen Red | 5000 | 1.23 |
| 1002 | PEN | Pen Blue | 8000 | 1.25 |
| 1003 | PEN | Pen Black | 2000 | 1.25 |
| 1004 | PEC | Pencil 2B | 10000 | 0.48 |
| 1005 | PEC | Pencil 2H | 8000 | 0.49 |
+-----------+-------------+-----------+----------+-------+
So if I run the command:
SELECT * FROM products GROUP BY productCode;
I get the following error:
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'southwind.products.productID' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
So from what I have gathered reading the mysql documentation, since there are duplicate values in my GROUP BY argument it throws an error, refusing to return something arbitrarily. I would rather not disable this so any help understanding what I am missing to make the query work is much appreciated.
Thank you.