I have a problem with my query and mysql throws the following error:
#1055 - Expression #66 of SELECT list is not in GROUP BY clause and contai
Not sure why no one emphasizing using ANY_VALUE() inbuilt function for the s.status column.
Check this out: https://dev.mysql.com/doc/refman/8.0/en/group-by-handling.html https://dev.mysql.com/doc/refman/8.0/en/miscellaneous-functions.html#function_any-value
If you want to use GROUP BY on specific column and get the id's of the results there is one nice trick how to do it:
SELECT `distinct_column`, MAX(`id`), MAX(`another_int_column`) FROM `table_name` GROUP BY `distinct_column`
Result:
distinct_column id another_int_column
Abertamy 13579 11
Adamov 17765 14
Adolfovice 2924 3
Achotín 2241 2
Babice 17772 14
Now you have distinct values from distinct_column and with it the values of the corresponding row.
source
I think that the id and another_int_column have to be unique, but i'm not sure.
When you use GROUP BY, you can use expressions in your select-list only if they have a single value per group. Otherwise you get ambiguous query results.
In your case, MySQL believes that s.status
might have multiple values per group. For example, you're grouping by p.products_id
but s.status
is a column in another table specials
, perhaps in a one-to-many relationship with table products
. So there might be multiple rows in specials
with the same products_id
, but different values for status
. If that's the case, which value for status
should the query use? It's ambiguous.
In your data, you might happen to limit the rows such that you only have one row in specials
for each row in products
. But MySQL can't make that assumption.
MySQL 5.6 and earlier let you write such ambiguous queries, trusting that you know what you're doing. But MySQL 5.7 enables more strict enforcement by default (this can be made less strict to behave like earlier versions).
The fix is to follow this rule: Every column in your select-list must fall into one of three cases:
GROUP BY
clause.GROUP BY
clause.For more explanation read this excellent blog: Debunking GROUP BY myths
Re your comment, I can only make a guess because you have not posted your table definitions.
I'm guessing that products_description
and manufacturers
are functionally dependent on products
, so it's okay to list them as is in the select-list. But this assumption may not be correct, I don't know your schema.
Anyway, the error about s.status
should be resolved by using an aggregate function. I'm using MAX()
as an example.
SELECT p.*,
pd.*,
m.*,
MAX(IF(s.status, s.specials_new_products_price, NULL))
AS specials_new_products_price,
MAX(IF(s.status, s.specials_new_products_price, p.products_price))
AS final_price
FROM products p
LEFT OUTER JOIN specials s ON p.products_id = s.products_id
INNER JOIN manufacturers m ON p.manufacturers_id = m.manufacturers_id
INNER JOIN products_description pd ON p.products_id = pd.products_id
INNER JOIN products_to_categories p2c ON p.products_id = p2c.products_id
INNER JOIN categories c ON p2c.categories_id = c.categories_id
WHERE p.products_view = 1
AND p.products_status = 1
AND p.products_archive = 0
AND c.virtual_categories = 0
AND pd.language_id = 1
GROUP BY p.products_id;
I also rewrote your joins in the proper way. Comma-style joins should be avoided.
Are you trying to do an ORDER BY
? The GROUP BY
clause attempts to return one row for each distinct value in the column in the GROUP BY
clause, unless you add more columns to the clause. The error is being thrown because you have other non-aggregated columns that are not being included in the clause - aggregated columns would be something like MAX(p.products_id)
or SUM(p.products_price)
. In your query, there are multiple rows with the same p.product_price and different s.status values, so the GROUP BY
isn't meaningful.
MySQL Group By handling: https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html Aggregate functions: http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html