Unknown Column In Where Clause

Corpo picture Corpo · Sep 30, 2008 · Viewed 278.4k times · Source

I have a simple query:

SELECT u_name AS user_name FROM users WHERE user_name = "john";

I get Unknown Column 'user_name' in where clause. Can I not refer to 'user_name' in other parts of the statement even after select 'u_name as user_name'?

Answer

dacracot picture dacracot · Sep 30, 2008

SQL is evaluated backwards, from right to left. So the where clause is parsed and evaluate prior to the select clause. Because of this the aliasing of u_name to user_name has not yet occurred.