SELECT
`model`,
`name`,
`category`,
`price`
FROM `books`
I want to go from:
********************************************************************
* model | name | category | price
********************************************************************
* x | y | categoryA | price
* b | d | categoryB | price
* y | u | categoryB | price
* d | u | categoryA | price
* d | u | categoryB | price
* d | u | categoryC | price
to:
********************************************************************
* model | name | categoryA price | categoryB price | categoryC price
********************************************************************
* x | y | price cat A | NULL | NULL
* b | d | NULL | price cat B | NULL
* y | u | NULL | price cat B | NULL
* d | u | price cat A | price cat B | NULL
I want to group model and name together and display their corresponding prices.
I've tried using left joins, subselects, case, if, etc... I am trying to find the most efficient way.
SELECT
`model`,
`name`,
(
case
when `category` = 'CategoryA'
then `price`
end
) as `CategoryA Price`,
(
case
when `category` = 'CategoryB'
then `price`
end
) as `CategoryB Price`
FROM `books`
GROUP BY `model`,`name`
However, I am not getting the wanted result.
I hope this crappy example gets my point across.
Thank you