Jam*_*mes 183 mysql sql mysql-error-1054
我正在运行的查询如下,但是我收到此错误:
#1054 - 'IN/ALL/ANY子查询'中的未知列'guaranteed_postcode'
SELECT `users`.`first_name`, `users`.`last_name`, `users`.`email`,
SUBSTRING(`locations`.`raw`,-6,4) AS `guaranteed_postcode`
FROM `users` LEFT OUTER JOIN `locations`
ON `users`.`id` = `locations`.`user_id`
WHERE `guaranteed_postcode` NOT IN #this is where the fake col is being used
(
SELECT `postcode` FROM `postcodes` WHERE `region` IN
(
'australia'
)
)
Run Code Online (Sandbox Code Playgroud)
我的问题是:为什么我无法在同一个数据库查询的where子句中使用假列?
vic*_*ugo 410
您只能在GROUP BY,ORDER BY或HAVING子句中使用列别名.
标准SQL不允许您引用WHERE子句中的列别名.施加此限制是因为当执行WHERE代码时,可能尚未确定列值.
从MySQL文档复制
正如评论中所指出的那样,使用HAVING可以完成工作.尽管如此,请务必阅读此WHERE与HAVING.
rod*_*ion 24
正如Victor指出的那样,问题在于别名.但是,通过将表达式直接放入WHERE x IN y子句中,可以避免这种情况:
SELECT `users`.`first_name`,`users`.`last_name`,`users`.`email`,SUBSTRING(`locations`.`raw`,-6,4) AS `guaranteed_postcode`
FROM `users` LEFT OUTER JOIN `locations`
ON `users`.`id` = `locations`.`user_id`
WHERE SUBSTRING(`locations`.`raw`,-6,4) NOT IN #this is where the fake col is being used
(
SELECT `postcode` FROM `postcodes` WHERE `region` IN
(
'australia'
)
)
Run Code Online (Sandbox Code Playgroud)
但是,我认为这是非常低效的,因为子查询必须为外部查询的每一行执行.
Jon*_*oni 20
标准SQL(或MySQL)不允许在WHERE子句中使用列别名,因为
在评估WHERE子句时,可能尚未确定列值.
(来自MySQL文档).您可以做的是计算WHERE子句中的列值,将值保存在变量中,并在字段列表中使用它.例如,你可以这样做:
SELECT `users`.`first_name`, `users`.`last_name`, `users`.`email`,
@postcode AS `guaranteed_postcode`
FROM `users` LEFT OUTER JOIN `locations`
ON `users`.`id` = `locations`.`user_id`
WHERE (@postcode := SUBSTRING(`locations`.`raw`,-6,4)) NOT IN
(
SELECT `postcode` FROM `postcodes` WHERE `region` IN
(
'australia'
)
)
Run Code Online (Sandbox Code Playgroud)
这避免了在表达式变得复杂时重复表达式,使代码更易于维护.
小智 14
也许我的答案为时已晚,但这可以帮助别人.
您可以使用另一个select语句将其括起来并使用where子句.
SELECT * FROM (Select col1, col2,...) as t WHERE t.calcAlias > 0
Run Code Online (Sandbox Code Playgroud)
calcAlias是计算的别名列.
| 归档时间: |
|
| 查看次数: |
180145 次 |
| 最近记录: |