"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Why does SQL query report "Unknown column in WHERE clause" error when using alias Times?

Why does SQL query report "Unknown column in WHERE clause" error when using alias Times?

Posted on 2025-04-12
Browse:202

Why Does My SQL Query Fail with

SQL query causes "Unknown Column In Where Clause" error due to alias

question:

Query using alias in a SELECT statement will cause an error, prompting that the unknown alias column in the WHERE statement. For example, the following query triggers this error:

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

explain:

SQL execution order is from right to left. In this example, the WHERE clause is executed before the SELECT clause. Therefore, when parsing the WHERE clause, the alias user_name is not yet defined.

Solution:

To solve this problem, you can use the original column name in the WHERE clause:

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

Or enclose the alias column names in brackets in the WHERE clause:

SELECT u_name AS user_name FROM users WHERE (user_name = "john");
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3