当前位置:网站首页>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:
边栏推荐
- 天翼云4.0分布式云赋能千行百业数字化转型
- Security First: Tools You Need to Know to Implement DevSecOps Best Practices
- Data Governance: The Evolution of Data Integration and Application Patterns
- 知识点滴 - 什么是iAP2 (上)
- golang 源码分析(39)hystrix-go
- CUDA+Pycharm-gpu版本+Anaconda安装
- LeetCode 1947. 最大兼容性评分和(状态枚举DP)
- 判断文件属主
- LeetCode 2349. 设计数字容器系统(SortedSet)
- HDF驱动框架的API(1)
猜你喜欢
随机推荐
golang刷leetcode动态规划(9)不同路径 II
MySQL基本查询和运算符
【软考软件评测师】基于经验的测试技术
shell中awk命令的if条件语句引入外置变量
MySQL表的约束
Openharmony - 基于ArkUI(TS)开发颜色选择器
MySQL基本语法
手机银行体验性测试:如何获取用户真实感受
Smart Microelectronics Releases Low-Power MM32L0130 Series MCU Products
NeRF: The Secret of 3D Reconstruction Technology in the Popular Scientific Research Circle
编译型语言与解释型语言的区别
【案例】2D变换-旋转动画
redis总结_基础
KunlunBase 1.0 发布了!
Go 语言快速入门指南:第二篇 变量与常量
redis summary_distributed cache
E-Surfing Cloud 4.0 Distributed Cloud Enables Digital Transformation of Thousands of Industries
golang刷leetcode 经典(2)拓扑排序
大事务故障案例
LeetCode 2353. 设计食物评分系统(sortedcontainers)









