I want to get out the minimum price for categories 1, 2 and 3
I’ve used
LEAST(MIN(price_reduced),MIN(price))
IFNULL(MIN(price_reduced),MIN(price)) ... WHERE price <> 0 and price_reduced <> 0
Database
id | category | price | price_reduced |
---|---|---|---|
1 | 1 | 200 | 100 |
2 | 1 | 300 | 0 |
3 | 1 | 500 | 0 |
4 | 2 | 200 | 150 |
5 | 2 | 125 | 0 |
6 | 3 | 300 | 0 |
7 | 3 | 200 | 90 |
Output
1 - 100 2 - 125 3 - 90
Thank you
2
Answers
Maybe with
cte
:Or without
cte
but with derived table:Or just a single query:
All return the same results.
Demo fiddle
This query will work on all MySQL V4+ :