oracle:你能为 from 子句指定一个别名吗?
你能为 from 子句指定一个别名吗?像:
select a - b "Markup" from retail a, cost b;
编辑:抱歉,我输入得太快了,并试图将问题简化到没有任何意义的程度。
我实际上想做的是使用别名来比较同一个表中两个发布日期之间的月份。这是我发现有效的:
select distinct to_char(months_between((select distinct pubdate
from books3
where pubid = 2),
(select distinct pubdate
from books3
where pubid = 4)), '99.99') "Answer"
from books3
我希望它看起来像这样:
select distinct months_between(a,b)
from (select distinct pubdate
from books3
where pubid = 2 as a),
(select distinct pubdate
from books3
where pubid = 4 as b)
但这不起作用
can you assign an alias to the from clause? like:
select a - b "Markup" from retail a, cost b;
EDIT: sorry i typed that out a bit too quick and tried to simplify the question to the point where it didnt make any sense
What im actually trying to do is use aliases to compare the months between two publishing dates in the same table. Here's what i found works:
select distinct to_char(months_between((select distinct pubdate
from books3
where pubid = 2),
(select distinct pubdate
from books3
where pubid = 4)), '99.99') "Answer"
from books3
i wanted it to looks something like this:
select distinct months_between(a,b)
from (select distinct pubdate
from books3
where pubid = 2 as a),
(select distinct pubdate
from books3
where pubid = 4 as b)
but that isn't working
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,Oracle 支持表别名。它支持
SELECT
列表中的AS
,但不支持FROM
列表:大多数数据库支持省略
AS
关键字。也就是说,表别名不是列别名——您仍然需要在 SELECT 子句中引用相应表中的特定列,就像您在我的示例更新中看到的那样。我还添加了
WHERE
标准,以便查询不会返回笛卡尔积。派生表/内联视图有时需要表别名(又名子查询,尽管我发现术语非常模糊):
这是您的查询:
您的问题是您将表别名放在派生表内,而它需要位于括号之外/括号:
您需要不同的原因是因为笛卡尔积。
Yes, Oracle supports table aliases. It supports
AS
in theSELECT
list but not in theFROM
list:Most databases support omitting the
AS
keyword.That said, table aliases aren't column aliases -- you still need to reference a specific column in the respective table in the SELECT clause, like you see in my update of your example. I also added the
WHERE
criteria so the query wouldn't be returning a Cartesian product.Table aliases are sometimes required for derived tables/inline views (AKA subquery, though I find the terminology very vague):
Here's your query:
Your problem was you were putting the table alias inside the derived table, when it needs to be outside the brackets/parenthesis:
The reason you need the distinct is because of the Cartesian product.
最接近您所拥有的是将
AS 别名
从子查询中移出但是,查询仍然没有多大意义。如果
pubid=2
有 2 条记录,pubid=4
有 3 条记录,则输出中有 6 行......我怀疑您实际上正在进行一些分组,因此这将在每个 bookid 级别上比较 pubid=2 和 pubid=4 条目。
The closest to what you have would be to move the
AS alias
out of the subqueryBut still, the query doesn't make much sense. If there are 2 records for
pubid=2
and 3 forpubid=4
, you get 6 rows in the output....I suspect you actually have some grouping going on, so this will compare pubid=2 and pubid=4 entries at a per-bookid level.