Ollivander's Inventory

  • + 6 comments

    MySQL. In the beginning I had trouble with the output because SELECT output wands with a minimum coins needed that didn't always match with that wand.

    There is a sub-query to make sure that if the wand age and wand power matches with another, to return the cheapest (lowest coins_needed) wand with.

    SELECT W.id, P.age, W.coins_needed, W.power
    FROM WANDS AS W
        INNER JOIN WANDS_PROPERTY AS P ON W.code = P.code
    WHERE P.is_evil = 0 AND W.coins_needed = 
        (SELECT MIN(coins_needed)
         FROM WANDS AS W1
            INNER JOIN WANDS_PROPERTY AS P1 ON W1.code = P1.code
         WHERE W1.power = W.power AND P1.age = P.age)
    ORDER BY W.power DESC, P.age DESC