简体   繁体   English

Where 子句中的未知列

[英]Unknown Column In Where Clause

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 .Unknown Column 'user_name' in where clause得到了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' ?即使在select 'u_name as user_name'之后,我也不能在语句的其他部分引用'user_name' select 'u_name as user_name'吗?

SQL is evaluated backwards, from right to left. SQL 从右到左向后求值。 So the where clause is parsed and evaluate prior to the select clause.所以 where 子句在 select 子句之前被解析和评估。 Because of this the aliasing of u_name to user_name has not yet occurred.因此,尚未发生 u_name 到 user_name 的别名。

关于什么:

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

See the following MySQL manual page: http://dev.mysql.com/doc/refman/5.0/en/select.html请参阅以下 MySQL 手册页: 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." “可以使用 AS alias_name 为 select_expr 指定一个别名。别名用作表达式的列名,并且可以在 GROUP BY、ORDER BY 或 HAVING 子句中使用。”

(...) (……)

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.不允许在 WHERE 子句中引用列别名,因为在执行 WHERE 子句时可能尚未确定列值。 See Section B.5.4.4, “Problems with Column Aliases”.请参见第 B.5.4.4 节“列别名问题”。

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.可以这样想,您的 where 子句首先评估,以确定需要返回哪些行(或连接的行)。 Once the where clause is executed, the select clause runs for it.一旦 where 子句被执行,select 子句就会为它运行。

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.哪里总是先求值,然后是 select 子句。

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 语句创建了一个实际上不存在于数据库中的新字段,并尝试使用该结果的别名你会遇到同样的问题:

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".您将收到错误“WHERE 子句中的未知列‘attachmentcount’”。

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.您仍然会返回别名,但现在 SQL 不应该在未知别名处出错。

Your defined alias are not welcomed by the WHERE clause you have to use the HAVING clause for this WHERE子句不欢迎您定义的alias ,您必须为此使用HAVING子句

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

OR you can directly use the original column name with the WHERE或者您可以直接使用原始列名和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与作为子查询或任何计算的结果在用户定义别名中的结果相同,它将由HAVING子句而不是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'

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.如果 RDBMS 支持将谓词推入内联视图,则后者应该与前者相同。

更正:

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

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. user_name 直到返回时间才存在。

由第 1 行和第 2 行引起并由第 3 行解决的WHERE子句中的未知列:

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

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将扫描 FULL TABLE - 您尚未指定 LIMIT 1 部分
  • So, - THIS QUERY WILL BE SLLLOOOOOOW on huge tables.所以, - 这个查询在大桌子上会很慢。

But, may be it helps in some cases但是,在某些情况下可能会有所帮助

While you can alias your tables within your query (ie, "SELECT u.username FROM users u;"), you have to use the actual names of the columns you're referencing.虽然您可以在您的查询中为您的表命名(即“SELECT u.username FROM users u;”),但您必须使用您所引用的列的实际名称。 AS only impacts how the fields are returned. AS 仅影响字段的返回方式。

Not as far as I know in MS-SQL 2000/5. 据我所知,在MS-SQL 2000/5中。 I've fallen foul of this in the past. 过去我对此一直犯规。

Just had this problem.刚遇到这个问题。

Make sure there is no space in the name of the entity in the database.确保数据库中实体的名称中没有空格。

eg ' user_name' instead of 'user_name'例如“user_name”而不是“user_name”

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

try your task using IN condition or OR condition and also this query is working on spark-1.6.x使用IN条件或OR条件尝试您的任务,此查询也在 spark-1.6.x 上运行

 SELECT  patient, patient_id FROM `patient` WHERE patient IN ('User4', 'User3');

or要么

SELECT  patient, patient_id FROM `patient` WHERE patient = 'User1' OR patient = 'User2';

For me the root of the problem was a number which I copied to use in a WHERE clause.对我来说,问题的根源是我复制到 WHERE 子句中使用的一个数字。 The number had "invisible" symbol, at least for MySQL Workbench.该数字具有“隐形”符号,至少对于 MySQL Workbench 而言是这样。 I placed the number in the Chrome console it was clearly visible.我将数字放在 Chrome 控制台中,它清晰可见。

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.记住将 $user 放在 ' ' 单引号中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM