mardi 1 septembre 2015

How to improve an IF statement in MySQL?

Consider the mysql

SELECT If(
            (SELECT `price` FROM `prices` AS `B` WHERE `A`.`Item_ID` = `B`.`Item_ID`)<5,
            5,
            (SELECT `price` FROM `prices` AS `B` WHERE `A`.`Item_ID` = `B`.`Item_ID`)
        ) AS `Item_Price' 
FROM `items` AS `A`

where it used to find the price from prices table for each item from the items table, and If the price is less than 5, then it is to 5. The problem with this is that if the IF statement comes false, i.e. the price greater or equal to 5, then it runs another search for the same price which may slow down the page. Is there a way to improve this?

Aucun commentaire:

Enregistrer un commentaire