Problem
I am using alternate column name (alias) in a Query, I can use the alias "given_name" as part of the ORDER BY but am unable to use it as part of the WHERE clause. The WHERE "given_name" is passed in as the result of a request out of my control and I do not know the actual column name that should be used in the WHERE condition.
Question
- It there a way/hack to use a column alias in a WHERE clause?
- Is there a way to find the column name from an alias?
Research
After some research it looks like alias are added after after the WHERE clause.
Example
SELECT profile.id AS id, given.name AS 'given_name', family.name AS 'family_name'
FROM green_profile profile
LEFT JOIN green_name given ON given.profileid = profile.id AND given.name_typeid = 0
LEFT JOIN green_name family ON family.profileid = profile.id AND family.name_typeid = 1
WHERE given_name LIKE 'levi%'
ORDER BY given_name DESC LIMIT 0 , 25