当前位置:网站首页>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:
边栏推荐
猜你喜欢
白话电子签章原理及风险
MySQL基本语法
LeetCode 2349. 设计数字容器系统(SortedSet)
阿波罗 planning代码-modules\planning\lattice\trajectory_generation\PiecewiseBrakingTrajectoryGenerator类详解
AI+医疗:使用神经网络进行医学影像识别分析
shell中awk命令的if条件语句引入外置变量
NIO基础之三大组件
TSF微服务治理实战系列(一)——治理蓝图
Simulink脚本自动创建Autosar Parameter Port及Mapping
How Tencent architects explained: The principle of Redis high-performance communication (essential version)
随机推荐
有关代购系统搭建的那点事
Five keys to a successful Industrial IoT deployment
LeetCode 2336. 无限集中的最小数字(SortedSet)
面试官:可以谈谈乐观锁和悲观锁吗
sed 命令
Playing in the cloud | The key technology of Tianyi cloud object storage ZOS high availability is revealed
查看数据库数据量大小,占用磁盘大小
白话电子签章原理及风险
无法超越的100米_百兆以太网传输距离_网线有哪几种?
Dream weaving prompt information prompt box beautification
Win11dll文件缺失怎么修复?Win11系统dll文件丢失的解决方法
MySQL基本查询和运算符
搭建属于自己的知识库(Wikijs)
CUDA+Pycharm-gpu版本+Anaconda安装
2022最新版SSM源码分析:一套教程助你深入理解底层原理,提高核心竞争力!
Cpolar application example of data acquisition equipment
golang刷leetcode 经典(2)拓扑排序
Data Governance: The Evolution of Data Integration and Application Patterns
LiveGBS国标GB/T28181流媒体平台支持主子码流切换主码流stream/streamprofile
织梦自定义表单添加全选和全不选功能按钮