Unknown Column In Where Clause

MysqlSqlMysql Error-1054

Mysql Problem Overview


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'?

Mysql Solutions


Solution 1 - Mysql

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.

Solution 2 - Mysql

What about:

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

Solution 3 - Mysql

See the following MySQL manual page: http://dev.mysql.com/doc/refman/5.0/en/select.html

> "A select_expr can be given an alias > using AS alias_name. The alias is used > as the expression's column name and > can be used in GROUP BY, ORDER BY, or > HAVING clauses."

(...)

> It is not permissible to refer to a column alias in a WHERE clause, > because the column value might not yet be determined when the WHERE > clause is executed. See Section B.5.4.4, “Problems with Column > Aliases”.

Solution 4 - Mysql

select u_name as user_name from users where u_name = "john";

Think of it like this, your where clause evaluates first, to determine which rows (or joined rows) need to be returned. Once the where clause is executed, the select clause runs for it.

To put it a better way, imagine this:

select distinct(u_name) as user_name from users where u_name = "john";

You can't reference the first half without the second. Where always gets evaluated first, then the select clause.

Solution 5 - Mysql

If you're trying to perform a query like the following (find all the nodes with at least one attachment) where you've used a SELECT statement to create a new field which doesn't actually exist in the database, and try to use the alias for that result you'll run into the same problem:

SELECT nodes.*, (SELECT (COUNT(*) FROM attachments 
WHERE attachments.nodeid = nodes.id) AS attachmentcount 
FROM nodes
WHERE attachmentcount > 0;

You'll get an error "Unknown column 'attachmentcount' in WHERE clause".

Solution is actually fairly simple - just replace the alias with the statement which produces the alias, eg:

SELECT nodes.*, (SELECT (COUNT(*) FROM attachments 
WHERE attachments.nodeid = nodes.id) AS attachmentcount 
FROM nodes 
WHERE (SELECT (COUNT(*) FROM attachments WHERE attachments.nodeid = nodes.id) > 0;

You'll still get the alias returned, but now SQL shouldn't bork at the unknown alias.

Solution 6 - Mysql

Your defined alias are not welcomed by the WHERE clause you have to use the HAVING clause for this

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

OR you can directly use the original column name with the WHERE

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

Same as you have the result in user defined alias as a result of subquery or any calculation it will be accessed by the HAVING clause not by the WHERE

SELECT u_name AS user_name ,
(SELECT last_name FROM users2 WHERE id=users.id) as user_last_name
FROM users  WHERE u_name = "john" HAVING user_last_name ='smith'

Solution 7 - Mysql

Either:

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

or:

SELECT user_name
from
(
SELECT u_name AS user_name
FROM   users
)
WHERE  u_name = "john";

The latter ought to be the same as the former if the RDBMS supports predicate pushing into the in-line view.

Solution 8 - Mysql

corrected:

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

Solution 9 - Mysql

No you need to select it with correct name. If you gave the table you select from an alias you can use that though.

Solution 10 - Mysql

No you cannot. user_name is doesn't exist until return time.

Solution 11 - Mysql

SELECT user_name
FROM
(
SELECT name AS user_name
FROM   users
) AS test
WHERE  user_name = "john"

Solution 12 - Mysql

Unknown column in WHERE clause caused by lines 1 and 2 and resolved by line 3:

  1. $sql = "SELECT * FROM users WHERE username =".$userName;
  2. $sql = "SELECT * FROM users WHERE username =".$userName."";
  3. $sql = "SELECT * FROM users WHERE username ='".$userName."'";

Solution 13 - Mysql

May be it helps.

You can

SET @somevar := '';
SELECT @somevar AS user_name FROM users WHERE (@somevar := `u_name`) = "john";

It works.

BUT MAKE SURE WHAT YOU DO!

  • Indexes are NOT USED here
  • There will be scanned FULL TABLE - you hasn't specified the LIMIT 1 part
  • So, - THIS QUERY WILL BE SLLLOOOOOOW on huge tables.

But, may be it helps in some cases

Solution 14 - Mysql

While you can alias your tables within your query (i.e., "SELECT u.username FROM users u;"), you have to use the actual names of the columns you're referencing. AS only impacts how the fields are returned.

Solution 15 - Mysql

Just had this problem.

Make sure there is no space in the name of the entity in the database.

e.g. ' user_name' instead of 'user_name'

Solution 16 - Mysql

I had the same problem, I found this useful.

mysql_query("SELECT * FROM `users` WHERE `user_name`='$user'");

remember to put $user in ' ' single quotes.

Solution 17 - Mysql

For me the root of the problem was a number which I copied to use in a WHERE clause. The number had "invisible" symbol, at least for MySQL Workbench. I placed the number in the Chrome console it was clearly visible.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCorpoView Question on Stackoverflow
Solution 1 - MysqldacracotView Answer on Stackoverflow
Solution 2 - MysqlSeptimusView Answer on Stackoverflow
Solution 3 - MysqlPaul DixonView Answer on Stackoverflow
Solution 4 - MysqlMark S.View Answer on Stackoverflow
Solution 5 - MysqlJonView Answer on Stackoverflow
Solution 6 - MysqlM Khalid JunaidView Answer on Stackoverflow
Solution 7 - MysqlDavid AldridgeView Answer on Stackoverflow
Solution 8 - MysqlSteven A. LoweView Answer on Stackoverflow
Solution 9 - MysqlPer Hornshøj-SchierbeckView Answer on Stackoverflow
Solution 10 - MysqlJarrett MeyerView Answer on Stackoverflow
Solution 11 - MysqlForipepeView Answer on Stackoverflow
Solution 12 - MysqloliyideView Answer on Stackoverflow
Solution 13 - MysqlF_SView Answer on Stackoverflow
Solution 14 - MysqlBlumerView Answer on Stackoverflow
Solution 15 - Mysqluser3103155View Answer on Stackoverflow
Solution 16 - MysqldevWaleedView Answer on Stackoverflow
Solution 17 - MysqlVi0nikView Answer on Stackoverflow