WHERE 语句中无法识别列别名
我创建的查询之一遇到了一个奇怪的“问题”。给出下一个查询:
SELECT
ID,
DistanceFromUtrecht,
(
SELECT
(MAX(DateUntil) - (ReleaseDays * 60 * 60 * 24))
FROM
PricePeriod
WHERE
PricePeriod.FK_Accommodation = Accommodation.ID
) AS LatestBookableTimestamp
FROM
Accommodation
WHERE
LatestBookableTimestamp < UNIX_TIMESTAMP()
phpMyAdmin 不断抛出一个关于没有名为“LatestBookableTimestamp”的列的错误,尽管我有一个由子查询检索的列,该别名。我还尝试过选择带有表前缀的每一列。这没有用八。最后,我通过表别名选择了所有列,并给了表一个别名。一切都没有运气。
有人可以告诉我我做错了什么吗?我什至搜索了一些资源,看看我是否弄错了,但在许多情况下,互联网上的作者使用与我相同的语法。
I have a strange 'problem' with one of my created queries. Given the next query:
SELECT
ID,
DistanceFromUtrecht,
(
SELECT
(MAX(DateUntil) - (ReleaseDays * 60 * 60 * 24))
FROM
PricePeriod
WHERE
PricePeriod.FK_Accommodation = Accommodation.ID
) AS LatestBookableTimestamp
FROM
Accommodation
WHERE
LatestBookableTimestamp < UNIX_TIMESTAMP()
phpMyAdmin keeps throwing an error about not having a column named 'LatestBookableTimestamp', even allthough I've a column, retreived by a subquery, that alias. I've also tried it selecting every column with the tableprefix. This didn't work eighter. Finally I've selected all columns by a table-alias and I gave the table an alias. All with no luck.
Can someone tell me what I'm doing wrong? I've even searched for some resources to see if I'm not mistaken, but in many cases authors on the internet use the same syntax as I do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 HAVING
顺便说一句,您正在使用依赖子查询,这在性能方面是一个坏主意。
尝试这样:
Use HAVING
On a side note, you're using a dependednt subquery, which is a bad idea performance wise.
Try like this:
不能在 WHERE 子句中使用列别名。
MySQL(和 SQL Server)将允许在 GROUP BY 中使用列别名,但并未得到广泛支持。 ORDER BY 是支持列别名引用最一致的地方。
使用:
You can't use a column alias in the WHERE clause.
MySQL (and SQL Server) will allow column alias use in the GROUP BY, but it's not widely supported. ORDER BY is the most consistently supported place that supports column alias referencing.
Use:
您需要采用不带 where 子句的原始查询,并将其转换为子查询。
You would need to take the original query, without the where clause, and turn that into a sub query.