当前位置:网站首页>SQL Alias Aliases
SQL Alias Aliases
2022-08-02 21:02:00 【night drift ice】
SQL aliases are used to give a table, or a column in a table, a temporary alias.
The effect is:
1, used to change the column names in the result set
2, after changing the alias, prevent name conflict, or simplify the name to make the meaning of the name more reasonable and clear.
This alias is only valid during ad hoc queries.
When creating an alias, use the AS keyword.
Create aliases for data columns:
SELECT column_name AS alias_name FROM table_name;
Create an alias for the data table:
SELECT column_name(s) FROM table_name AS alias_name;
Example:
SELECT customer_id AS ID FROM Customers;
Search results:
ID |
1 |
2 |
3 |
4 |
5 |
Note that if you create an alias with spaces in it, use double quotes or square brackets to enclose the name.(double quotation marks or square brackets)
When aliases are usually used, we have made a certain combination of data in multiple columns, as shown below, combining address, zip code, city, and country as a new address.
SELECT CustomerName, Address + ', ' + PostalCode + ' ' + City + ', ' + Country AS Address
FROM Customers;
If it is a MySQL database:
SELECT CustomerName, CONCAT(Address,', ',PostalCode,', ',City,', ',Country) AS Address
FROM Customers;
If it is an Oracle database:
SELECT CustomerName, (Address || ', ' || PostalCode || ' ' || City || ', ' || Country) AS Address
FROM Customers;
An example of using table aliases is as follows.
SELECT o.OrderID, o.OrderDate, c.CustomerName
FROM Customers AS c, Orders AS o
WHERE c.CustomerName='Around the Horn' AND c.CustomerID=o.CustomerID;
This example uses two tables for joint query, using aliases to shorten the name of the table, which is more convenient to use.
In summary, the usage of aliases in SQL is:
1, when there are multiple tables in the query, or the same table SELF JOIN.
2, to use a function in a query, the result needs to be calculated and named as a column.
3, the column name is too long to read or type.
4, when the data of multiple columns is to be combined together.
Reference:
边栏推荐
猜你喜欢
随机推荐
golang刷leetcode 经典(3) 设计推特
Go 语言快速入门指南: 介绍及安装
How a "cloud" can bring about new changes in the industry
LeetCode 2353. 设计食物评分系统(sortedcontainers)
危及安全的常见物联网攻击有哪些?
NeRF: The Secret of 3D Reconstruction Technology in the Popular Scientific Research Circle
vulnhub W34kn3ss: 1
织梦自定义表单添加全选和全不选功能按钮
下载mysql的源码包
故障分析 | 一条 SELECT 语句跑崩了 MySQL ,怎么回事?
深圳地铁16号线二期进入盾构施工阶段,首台盾构机顺利始发
golang源码分析(33)pollFD
TSF微服务治理实战系列(一)——治理蓝图
共享平台如何提高财务的分账记账效率?
redis总结_基础
开源一夏 |【云原生】DevOps(五):集成Harbor
golang刷leetcode 字符串(4)逆波兰式
C#里如何简单的校验时间格式
租房小程序自动定位城市
golang刷leetcode 经典(2)拓扑排序