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'
?
May be it helps.
You can
It works.
BUT MAKE SURE WHAT YOU DO!
But, may be it helps in some cases
No you need to select it with correct name. If you gave the table you select from an alias you can use that though.
No you cannot. user_name is doesn't exist until return time.
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:
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:
You'll still get the alias returned, but now SQL shouldn't bork at the unknown alias.
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'