由 UNION 形成的表的列名
给定几个简单的表,如下所示:
create table R(foo text);
create table S(bar text);
如果我要在查询中将它们联合在一起,我该如何称呼该列?
select T.????
from (
select foo
from R
union
select bar
from S) as T;
现在,在 mysql 中,我显然可以将 T 的列称为“foo”——联合中第一个关系的匹配列的名称。 然而,在 sqlite3 中,这似乎不起作用。 有没有一种方法可以在所有 SQL 实现中都是标准的?
如果没有,那么只使用 sqlite3 怎么样?
更正:毕竟 sqlite3 确实允许您将 T 的列称为“foo”! 哎呀!
Given a couple of simple tables like so:
create table R(foo text);
create table S(bar text);
If I were to union them together in a query, what do I call the column?
select T.????
from (
select foo
from R
union
select bar
from S) as T;
Now, in mysql, I can apparently refer to the column of T as 'foo' -- the name of the matching column for the first relation in the union. In sqlite3, however, that doesn't seem to work. Is there a way to do it that's standard across all SQL implementations?
If not, how about just for sqlite3?
Correction: sqlite3 does allow you to refer to T's column as 'foo' after all! Oops!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试给列起一个别名;
或者如果列名不是必需的,那么 T.* 就足够了。
Try to give an alias to columns;
or If the name of column is not necessary then T.* will be enough.
尽管没有拼写规则,但我们可以使用联合查询中第一个子查询的列名来获取联合结果。
Although there is no spelled rule, we can use the column names from the first subquery in the union query to fetch the union results.
您只需要在第一次选择时使用列别名(在 SQl Server 2008 R2 中测试)
you only need column aliases only in first select (tested in SQl Server 2008 R2)