Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

PostgreSQL AS


别名

SQL 别名用于为表或表中的列提供一个临时名称。

别名通常用于使列名更易读。

别名仅在该查询持续期间存在。

别名使用 AS 关键字创建。

示例

为列使用别名

SELECT customer_id AS id
FROM customers;
运行示例 »

AS 是可选的

实际上,您可以跳过 AS 关键字并获得相同的结果

示例

没有 AS 的相同结果

SELECT customer_id id
FROM customers;
运行示例 »

连接列

当两个或多个字段连接成一个时,通常使用 AS 关键字。

要连接两个字段,请使用 ||

示例

连接两个字段并将其称为 product

SELECT product_name || unit AS product
FROM products;
运行示例 »

注意:在上面示例的结果中,我们在 product_name 和 unit 之间缺少空格。要在连接时添加空格,请使用 || ' ' ||

示例

连接,带空格

SELECT product_name || ' ' || unit AS product
FROM products;
运行示例 »

使用带空格的别名

如果希望您的别名包含一个或多个空格,例如 "My Great Products",请将您的别名用双引号括起来。

示例

用双引号括起您的别名

SELECT product_name AS "My Great Products"
FROM products;
运行示例 »

PostgreSQL 练习

通过练习测试自己

练习

customer_name 字段创建别名,使其显示为 Company

SELECT customer_name  FROM customers;
        

开始练习


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.