SQL: Queries: Table Expressions: Alias

 20th August 2020 at 2:19pm

别名。

表别名的写法:

FROM <table_reference> AS <alias>
FROM <table_reference> <alias>

一般用来简化长表名:

SELECT * FROM some_very_long_table_name s JOIN another_fairly_long_name a ON s.id = a.num;

一旦用了别名则不再可以使用原表名:

SELECT * FROM my_table AS m WHERE my_table.a > 5;    -- wrong

别名一般是用来简化语句。但表与自身做 JOIN 时就必须用别名

SELECT * FROM people AS mother JOIN people AS child ON mother.id = child.mother_id;

别名的优先级高于 JOIN:

--- my_table as b
SELECT * FROM my_table AS a CROSS JOIN my_table AS b ...

--- join result as b
SELECT * FROM (my_table AS a CROSS JOIN my_table) AS b ...

列别名不重要。