I have an sql query like this:
SELECT IF( (SELECT COUNT(*) FROM `test_table2` WHERE `id` = t.`id`) > 0,
(t.`price` + (SELECT `price` FROM `attribute` WHERE `id` = t.`id` LIMIT 1)),
t.`price`
) AS `full_price`,
MIN(`full_price`) as `min`, MAX(`full_price`) as `max`
FROM `test_table` t
How can i retrieve min and max values without using duplicate code in if statement?
Thanks.
Enclose the calculation of
full_pricein a subquery:Improvements:
CASEclause and notIFwhich is only for MySQL.(SELECT COUNT ...) > 0toEXISTS (SELECT ... ). It’s usually faster.Your query would become: