当前位置:网站首页>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:
边栏推荐
- Simulink脚本自动创建Autosar Parameter Port及Mapping
- NeRF: The Secret of 3D Reconstruction Technology in the Popular Scientific Research Circle
- 玩转云端 | 天翼云对象存储ZOS高可用的关键技术揭秘
- golang刷leetcode 经典(1) LRU缓存机制
- 下载mysql的源码包
- 查看数据库数据量大小,占用磁盘大小
- IDEA相关配置(特别完整)看完此篇就将所有的IDEA的相关配置都配置好了、设置鼠标滚轮修改字体大小、设置鼠标悬浮提示、设置主题、设置窗体及菜单的字体及字体大小、设置编辑区主题、通过插件更换主题
- Go 语言快速入门指南:第二篇 变量与常量
- 注释
- 共享平台如何提高财务的分账记账效率?
猜你喜欢

Redis总结_实战篇

What is the difference between erp system and wms system

redis总结_基础

天翼云4.0来了!千城万池,无所不至!

AI+医疗:使用神经网络进行医学影像识别分析

Playing in the cloud | The key technology of Tianyi cloud object storage ZOS high availability is revealed

如何确保智能工厂的安全?

Win11dll文件缺失怎么修复?Win11系统dll文件丢失的解决方法

redis summary_distributed cache
![Open Source Summer | [Cloud Native] DevOps (5): Integrating Harbor](/img/db/16ae82217382e72824a4b454060833.png)
Open Source Summer | [Cloud Native] DevOps (5): Integrating Harbor
随机推荐
redis总结_基础
新特性解读 | MySQL 8.0 GIPK 不可见主键
C# 术语
今年上半年,我国公路建设总体形势持续向好
影响PoE供电传输距离的除了网线还有啥?
白话电子签章原理及风险
千万级QPS下服务如何才能平滑启动
Dream weaving prompt information prompt box beautification
一文看懂推荐系统:概要01:推荐系统的基本概念
Interviewer: can you talk about optimistic locking and pessimistic locks
如何构建准实时数仓?
成功部署工业物联网的五个关键
Open Source Summer | [Cloud Native] DevOps (5): Integrating Harbor
攻防世界-favorite_number
MySQL基本查询和运算符
浅谈混迹力扣和codeforces上的几个月
shell中awk命令的if条件语句引入外置变量
织梦自定义表单添加全选和全不选功能按钮
我的递归从不爆栈
深入理解IO流(第一篇)