如何将 MySQL 中不明确的别名解析为当前表的定义

发布于 2024-12-08 12:23:00 字数 370 浏览 2 评论 0原文

我想知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遥远的她 2024-12-15 12:23:00

通常,这就是我在这些情况下所做的:

SELECT revenue, 
       units, 
       revenue / units AS revperunit2 
FROM   (SELECT SUM(revenue) AS revenue, 
               SUM(units)   AS units 
        FROM   inputtable) subsel 

有时您有复杂的查询,并且重新声明字段不利于可读性,也不利于性能。子查询是要走的路。

Usually, this is what I do on these cases:

SELECT revenue, 
       units, 
       revenue / units AS revperunit2 
FROM   (SELECT SUM(revenue) AS revenue, 
               SUM(units)   AS units 
        FROM   inputtable) subsel 

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.

茶色山野 2024-12-15 12:23:00

由于非常具体的原因,您所描述的内容在 SQL 中是不可能的。在 SELECT 列表中,所有列都将同时计算(创建、解析),或者就像同时计算(创建、解析)一样。或者换句话说,无论计算顺序如何,都会产生相同的结果。

这意味着在另一列的计算中不能使用(引用)列别名。

如果您所描述的内容是可能的,我们可以拥有以下类型的循环引用:

SELECT SUM(a)*sb AS sa
     , SUM(b)*sa AS sb 
FROM aTable

当然应该将其作为错误拒绝。通过在查询解析器中添加一个检查循环引用的级别(存在循环引用:拒绝查询),这将相当容易,

但是应该如何计算结果:

SELECT SUM(a) AS b
     , SUM(b) AS a
FROM aTable

或者为什么应该拒绝它?

如果表中没有列 ab ,好吧,这很容易,(存在循环引用:拒绝查询)。但是如果有列ab,它是否是循环引用? SUM(b) 中的 b 是一个不明确的引用吗?它引用列 b 还是别名 b


顺便说一句,第二个是有效的 SQL 查询。因为 SQL 假定所有计算均已完成(此处为 SUM(a)SUM(b)),然后将别名分配给结果。它与: 相同,

UPDATE aTable
SET a=b
  , b=a

它实际上交换了两列中的数据。当你这样写的时候,它不是很清楚,直到你尝试一下。有一种更漂亮的替代方法可以更好地了解后台发生的情况(并非所有 RDBMS 都支持此功能):

UPDATE aTable
SET (a,b)=(b,a)

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:

SELECT SUM(a)*sb AS sa
     , SUM(b)*sa AS sb 
FROM aTable

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:

SELECT SUM(a) AS b
     , SUM(b) AS a
FROM aTable

or why should it be rejected?

If there are no columns a and b in the table, OK, that would be easy, (exists circular reference: reject query). But if there are columns a and b, is it a circular reference or not? Is b in SUM(b) an ambiguous reference? Does it refer to column b or to alias b ?


The second is a valid SQL query, by the way. Because SQL assumes that all calculations are done (SUM(a) and SUM(b) here) and then the aliases are assigned to the results. It's the same with:

UPDATE aTable
SET a=b
  , b=a

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):

UPDATE aTable
SET (a,b)=(b,a)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文