当前位置:网站首页>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:
边栏推荐
猜你喜欢

Cpolar application example of data acquisition equipment

IReport常见问题及处理方法

阿波罗 planning代码-modules\planning\lattice\trajectory_generation\PiecewiseBrakingTrajectoryGenerator类详解

AtomicInteger详解

灵动微电子发布低功耗 MM32L0130 系列 MCU 产品

Redis总结_实战篇

0725-面试记录

【案例】2D变换-旋转动画

LeetCode 2353. 设计食物评分系统(sortedcontainers)

LeetCode 2349. 设计数字容器系统(SortedSet)
随机推荐
CUDA+Pycharm-gpu版本+Anaconda安装
白话电子签章原理及风险
Win11主题下载一直转圈怎么办?Win11主题下载一直转圈的解决方法
golang 源码分析(39)hystrix-go
企业云成本管控,你真的做对了吗?
租房小程序自动定位城市
CWE4.8:2022年危害最大的25种软件安全问题
SQL Alias 别名
golang刷leetcode 经典(4) 实现跳表
搭建属于自己的知识库(Wikijs)
知识点滴 - 什么是iAP2 (上)
发挥云网融合优势,天翼云为政企铺设数字化转型跑道
KunlunBase 1.0 发布了!
WPF使用Prism登录
“12306”的架构到底有多牛逼?
Simulink脚本自动创建Autosar Parameter Port及Mapping
LeetCode 2353. 设计食物评分系统(sortedcontainers)
golang源码分析(33)pollFD
How Tencent architects explained: The principle of Redis high-performance communication (essential version)
【案例】2D变换-旋转动画