I have this select statement working fine:
SELECT
pre, dict.char, post, score
, (SELECT @res := MatchPre(pre, 'test') AS var) AS mpre
FROM dict;
It returns all of the data properly, with the result from MatchPre in a column labeled “mpre”. However, I want to only get results where mpre > 0. So, logically, I tried this:
SELECT
pre, dict.char, post, score
, (SELECT @res := MatchPre(pre, 'test') AS var) AS mpre
FROM dict
WHERE mpre > 0;
But to no avail. I’ve tried swapping “WHERE mpre > 0” with “WHERE @res > 0”, “WHERE @res.mpre > 0”, “WHERE mpre.@res > 0”, etc, but it either throws an error or returns an empty result set despite the original where-less query giving rows with mpre values greater than 0.
What is the correct syntax for this?
You cannot use aliases in a
whereclause.You will have to use a
havingclause or put the select with the alias inside another outer select.This is because when the
whereclause is run, the alias is not yet evaluated.The
havingrun after all theselect,where,group by,fromandjoinsare finished and does not suffer this limitation.Having cannot use an index though.
If you want to use an index use a sub-select.