I have a webapp that uses DataTables with server side processing. I have some SQL Queries that arbitrarily regroups data in columns, so I get queries like this:
SELECT case `column`
WHEN 'value1' then 'group1'
WHEN 'value2' then 'group1'
ELSE `column` END AS `column grouped`, SUM(`2010`)
GROUP BY `colonne grouped`
Now, I add some filtering based on user input, and I do like this:
SELECT case `column`
WHEN 'value1' then 'group1'
WHEN 'value2' then 'group1'
ELSE `column` END AS `column grouped`, SUM(`2010`)
GROUP BY `column grouped`
HAVING `column grouped` LIKE '%test%'
And it still work just fine, but my woes come when I try to make the whole thing case insentive, and so I do:
SELECT case `column`
WHEN 'value1' then 'group1'
WHEN 'value2' then 'group1'
ELSE `column` END AS `column grouped`, SUM(`2010`)
GROUP BY `colonne grouped`
HAVING UPPER(`column grouped`) LIKE '%test%'
And now I get "#1054 - Unknown column 'column grouped' in 'having clause'"
AS a workaround, I do something like this:
SELECT case `column`
WHEN 'value1' then 'group1'
WHEN 'value2' then 'group1'
ELSE `column` END AS `column grouped` , SUM(`2010`)
GROUP BY `colonne grouped`
HAVING UPPER(case `column`
WHEN 'value1' then 'group1'
WHEN 'value2' then 'group1'
ELSE `column` END) LIKE '%test%'
But this is not really convenient. Anyone knows why I'm not able to use UPPER with custom alias ?
I am using MySQL 5.5 by the way.