如何将 MySQL 中不明确的别名解析为当前表的定义
我想知道 MySQL 是否为您正在构建的当前表有某种默认别名(例如 SmallTalk 中的“self”或 Java 中的“this”)。例如:
SELECT
SUM(revenue) AS revenue,
SUM(units) AS units,
SUM(revenue)/SUM(units) AS revPerUnit,
-- I want to replace the above line with the below line:
self.revenue/self.units AS revPerUnit2
FROM inputTable;
我知道我可以使用明确的别名,但我想知道是否有一种方法可以指定当存在不明确的定义时您需要当前表的别名。
I was wondering if MySQL has some kind of default alias for the current table you are building (like 'self' in SmallTalk or 'this' in Java). For instance:
SELECT
SUM(revenue) AS revenue,
SUM(units) AS units,
SUM(revenue)/SUM(units) AS revPerUnit,
-- I want to replace the above line with the below line:
self.revenue/self.units AS revPerUnit2
FROM inputTable;
I know that I could just use unabiguous aliases, but I want to know if there is a way to specify that you want the current table's alias when there exists an ambiguous definition.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,这就是我在这些情况下所做的:
有时您有复杂的查询,并且重新声明字段不利于可读性,也不利于性能。子查询是要走的路。
Usually, this is what I do on these cases:
Sometimes you have complicated queries and re-declaring the fields is not good for readability neither is for performance. Sub queries is the way to go.
由于非常具体的原因,您所描述的内容在 SQL 中是不可能的。在
SELECT
列表中,所有列都将同时计算(创建、解析),或者就像同时计算(创建、解析)一样。或者换句话说,无论计算顺序如何,都会产生相同的结果。这意味着在另一列的计算中不能使用(引用)列别名。
如果您所描述的内容是可能的,我们可以拥有以下类型的循环引用:
当然应该将其作为错误拒绝。通过在查询解析器中添加一个检查循环引用的级别(存在循环引用:拒绝查询),这将相当容易,
但是应该如何计算结果:
或者为什么应该拒绝它?
如果表中没有列
a
和b
,好吧,这很容易,(存在循环引用:拒绝查询)。但是如果有列a
和b
,它是否是循环引用?SUM(b)
中的b
是一个不明确的引用吗?它引用列b
还是别名b
?顺便说一句,第二个是有效的 SQL 查询。因为 SQL 假定所有计算均已完成(此处为
SUM(a)
和SUM(b)
),然后将别名分配给结果。它与: 相同,它实际上交换了两列中的数据。当你这样写的时候,它不是很清楚,直到你尝试一下。有一种更漂亮的替代方法可以更好地了解后台发生的情况(并非所有 RDBMS 都支持此功能):
What you describe is not possible in SQL for a very specific reason. In a
SELECT
list, all columns are to de calculated (created, resolved) at the same time or as if they were calculated (created, resolved) at the same time. Or in other words, produce the same result in whatever order they are calculated.Which means no column alias can be used (referenced) in another column's calculation.
If what you describe was possible, we could have circular references of the type:
which off course should be rejected as an error. And that would be rather easy by adding a level in the query parser that checks for circular references (exists circular reference: reject query)
But how should the result of this be calculated:
or why should it be rejected?
If there are no columns
a
andb
in the table, OK, that would be easy, (exists circular reference: reject query). But if there are columnsa
andb
, is it a circular reference or not? Isb
inSUM(b)
an ambiguous reference? Does it refer to columnb
or to aliasb
?The second is a valid SQL query, by the way. Because SQL assumes that all calculations are done (
SUM(a)
andSUM(b)
here) and then the aliases are assigned to the results. It's the same with:which actaully swaps data in the two columns. When you write it this way, it's not very clear, until you try it out. There's an alternative prettier way that gives better the idea of what is happening in the background (not all RDBMS support this yet):