I am having the following problem. I want to execute as sql statement that filters the results with HAVING. However the having is on a column that is calculated from an IF() function inside the select. This way, MySQL server complains that the column inside having clause is unknown!
SELECT col1,col2,IF(expr1,expr2,expr3) AS `wantedColumn`
It is as mysql cannot understand that the column returned by the if expression is named wantedColumn... If I use other columns it is working correctly. But I need to filter on that. Any suggestions? Thanks
Best How To :
Answer by comment from Inanda Menezes @Inanda:
Look at it: sqlfiddle.com/#!2/5294e4/6 , I just removed the backticks from the alias used inside the having and your example worked. According to mysql documentation you don't need to use backticks on identifiers that does not have special words or characters. If you use it, in most of cases, it will not cause your query to fail, but it's not necessary at all to quote normal identifiers with non-standard escape. Anyway, it seems to fail when escaping with backsticks a alias in having clause which uses length function, so just don't escape this one. – Inanda Menezes