[英]SQL: Error in LIKE column for WHERE clause
I have this query: 我有这个查询:
SELECT `donations`.*, `contacts`.`first_name` AS contact_first_name, `contacts`.`last_name` AS
contact_last_name
FROM (`donations`)
LEFT OUTER JOIN `contacts` contacts ON `contacts`.`id` =
`donations`.`contact_id`
WHERE
`contact_first_name` LIKE
'%test%'
However I am getting error Unknow column contact_first_name in WHERE clause
. 但是我收到错误
Unknow column contact_first_name in WHERE clause
。 I see that I am selecting first name as alias contact_first_name
but still getting error. 我看到我在选择名字作为别名
contact_first_name
但仍然出现错误。
Can anyone help as to what is wrong I am doing here ? 有人可以帮我解决我在这里做错什么吗? Thanks
谢谢
Use this instead: 使用此代替:
SELECT `donations`.*, `contacts`.`first_name` AS contact_first_name, `contacts`.`last_name` AS
contact_last_name
FROM (`donations`)
LEFT OUTER JOIN `contacts` contacts ON `contacts`.`id` =
`donations`.`contact_id`
WHERE
`contacts`.`first_name` LIKE
'%test%'
The reason for this is because contacts.first_name AS contact_first_name
is being evaluated last so WHERE
does not know about the alias contact_first_name
Alternatively you can do: 原因是因为
contacts.first_name AS contact_first_name
的评估最后一次,所以WHERE
不知道别名contact_first_name
替代方法是:
SELECT *
FROM (
SELECT `donations`.*, `contacts`.`first_name` AS contact_first_name, `contacts`.`last_name` AS
contact_last_name
FROM (`donations`)
LEFT OUTER JOIN `contacts` contacts ON `contacts`.`id` =
`donations`.`contact_id`
) a
WHERE `contact_first_name` LIKE '%test%'
Change 更改
WHERE
`contact_first_name` LIKE
'%test%'
to 至
WHERE
`contacts`.`first_name` LIKE
'%test%'
The reason for this is that you can't use aliases in the WHERE
clause. 原因是您不能在
WHERE
子句中使用别名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.