The idea here is that the GROUP_CONCAT compiles a list of option codes out of the stock table joined to the option_stock and options tables, grouped by the stock id. An example row is this:
Name Options Transmission
'Holden Commodore' '111, 145, 166, 188' 'Auto'
This view works as it is, but I can't help but feel there's a more elegant solution?
CREATE VIEW stock_view AS
(select s.description AS Name,
group_concat(o.option_code order by o.option_code ASC separator ', ')
AS Options,
(case
WHEN group_concat(o.option_code) LIKE '%111%' then 'Auto'
WHEN group_concat(o.option_code) LIKE '%112%' then 'Manual'
else 'Other'
end) as Transmission
from stock s
join option_stock ost ON s.id = ost.stock_id
join options o ON o.id = ost.option_id
group by s.id)
I'm trying to avoid using this ugly looking GROUP_CONCAT inside CASE predicament, but I get an error saying the field Options
does not exist if I use it inside the case statement like this:
WHEN `Options` LIKE '%111%' then 'Auto'
I know why the error is thrown - it's because you can't use the alias of another column in this manner. But is there a way around it?