I have two tables: products
and prices
products
- id (PK)
- name
prices
- id (PK)
- product_id (FK > products)
- price
- originalPrice
Each product might have multiple prices. What I want to achieve is a query that returns me all products on-sale with its cheapest price
.
on-sale
= price < originalPrice- if a product is not
on-sale
, it should not be included in the results - if a product has multiple prices that qualify for
on-sale
, only return the cheapest price.
The resulting table should have these columns
- products.id
- products.name
- prices.id
- prices.price
- prices.originalPrice
With my attempts I'm ending up with this issue: #1055 - Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'tbl.price' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
. Please note that I cannot change the config.
MySQL version: 5.7.22
I have uploaded a SQL export with sample data here: https://www.dropbox.com/s/6ucdv6592dum6n6/stackoverflow_export.sql?dl=0