SQL 如何使用“as”关键字为 Oracle 中的表设置别名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21145028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 00:45:08  来源:igfitidea点击:

How to use the 'as' keyword to alias a table in Oracle?

sqloracletable-alias

提问by Marcos

I'm trying to execute this query in Oracle SQL Developer:

我正在尝试在 Oracle SQL Developer 中执行此查询:

SELECT G.Guest_ID, G.First_Name, G.Last_Name
FROM Guest AS G
  JOIN Stay AS S ON G.Guest_ID = S.Guest_ID
WHERE G.City = 'Miami' AND S.Room = '222';

However, I get the following error:

但是,我收到以下错误:

ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:
Error at Line: 2 Column: 12

I don't see any problem in line 2 and the error is not very descriptive. It appears to be something to do with the askeyword. If I remove it, it works fine. However, I want my queries to be very verbose. Therefore, I must figure out a way to fix whatever the problem is without removing the askeyword.

我在第 2 行中没有看到任何问题,并且该错误的描述性不强。它似乎与as关键字有关。如果我删除它,它工作正常。但是,我希望我的查询非常详细。因此,我必须想办法在不删除as关键字的情况下解决问题。

This is the structure of the tables involved:

这是所涉及的表的结构:

CREATE TABLE GUEST
(
  GUEST_ID       NUMBER               NOT NULL,
  LAST_NAME      VARCHAR2(50 BYTE),
  FIRST_NAME     VARCHAR2(50 BYTE),
  CITY           VARCHAR2(50 BYTE),
  LOYALTY_NUMBER VARCHAR2(10 BYTE)    
);

CREATE TABLE STAY
(
  STAY_ID        NUMBER                         NOT NULL,
  GUEST_ID       NUMBER                         NOT NULL,
  HOTEL_ID       NUMBER                         NOT NULL,
  START_DATE     DATE,
  NUMBER_DAYS    NUMBER, 
  ROOM           VARCHAR2(10 BYTE)
);

Thanks for any help in advance.

提前感谢您的任何帮助。

回答by Denys Séguret

You can use ASfor table aliasing on many SQL servers (at least MsSQL, MySQL, PostrgreSQL) but it's always optional and on Oracle it's illegal.

您可以AS在许多 SQL 服务器(至少 MsSQL、MySQL、PostrgreSQL)上使用表别名,但它始终是可选的,而在 Oracle 上它是非法的。

So remove the AS:

所以删除AS

SELECT G.Guest_ID, G.First_Name, G.Last_Name
FROM Guest G

回答by Kokila Mohan

Omit the ASfor table alias in Oracle.

AS在 Oracle 中省略for 表别名。

SELECT G.Guest_ID, G.First_Name, G.Last_Name
FROM Guest G
  JOIN Stay S ON G.Guest_ID = S.Guest_ID
WHERE G.City = 'Miami' AND S.Room = '222';

This will give you the output without errors.

这将为您提供没有错误的输出。