当前位置:网站首页>SQL Alias 别名
SQL Alias 别名
2022-08-02 17:57:00 【夜流冰】
SQL aliases用来给一个表,或者表中的某一列一个临时的别名。
这样作用是:
1,用来改变结果集中的列名
2,改了别名后,防止名字冲突,或者简化名字,使名字的意义更合理清晰。
这个别名只在再临时查询期间是有效的。
创建别名时,使用AS关键字。
给数据列创建别名:
SELECT column_name AS alias_name FROM table_name;
给数据表创建别名:
SELECT column_name(s) FROM table_name AS alias_name;
举例:
SELECT customer_id AS ID FROM Customers;
查询结果:
ID |
1 |
2 |
3 |
4 |
5 |
注意,如果创建的别名里面有空格,要使用双引号或方括号将名字包含起来。(double quotation marks or square brackets)
通常使用别名的时候,是我们对多个列的数据进行了一定的组合,如下所示,将地址、邮编、城市、国家组合起来作为新的地址。
SELECT CustomerName, Address + ', ' + PostalCode + ' ' + City + ', ' + Country AS Address
FROM Customers;
如果是MySQL数据库的话:
SELECT CustomerName, CONCAT(Address,', ',PostalCode,', ',City,', ',Country) AS Address
FROM Customers;
如果是Oracle数据库的话:
SELECT CustomerName, (Address || ', ' || PostalCode || ' ' || City || ', ' || Country) AS Address
FROM Customers;
使用table的别名的例子如下。
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;
这个例子使用了两个表进行联合查询,使用别名使表的名字变短,使用起来更方便。
综上,SQL中别名的使用情况是:
1,在查询中有多个表,或同一个表SELF JOIN时。
2,在查询中使用函数,需要运算得到结果,为其命名为一列。
3,列的名字太长不好阅读或输入时。
4,多个列的数据要组合到一起时。
参考:
边栏推荐
- 2022最新版SSM源码分析:一套教程助你深入理解底层原理,提高核心竞争力!
- Mini Program Graduation Works WeChat Gymnasium Reservation Mini Program Graduation Design Finished Product (8) Graduation Design Thesis Template
- Mysql和Redis如何保证数据一致性
- IReport常见问题及处理方法
- How can services start smoothly under tens of millions of QPS
- Endanger the safety of common Internet attacks have?
- Smart Microelectronics Releases Low-Power MM32L0130 Series MCU Products
- 基于HDF的LED驱动程序开发(1)
- redis summary_distributed cache
- 0725-面试记录
猜你喜欢
随机推荐
HDF驱动框架的API(2)
故障分析 | 一条 SELECT 语句跑崩了 MySQL ,怎么回事?
天翼云4.0分布式云赋能千行百业数字化转型
KunlunBase 1.0 发布了!
开源一夏 |【云原生】DevOps(五):集成Harbor
leetcode:622. 设计循环队列【循环队列板子】
Mini Program Graduation Works WeChat Gymnasium Reservation Mini Program Graduation Design Finished Product (8) Graduation Design Thesis Template
Go 语言快速入门指南:第二篇 变量与常量
基于HDF的LED驱动程序开发(1)
MySQL索引
常用随机变量的数学期望和方差
“12306”的架构到底有多牛逼?
今年上半年,我国公路建设总体形势持续向好
How can services start smoothly under tens of millions of QPS
redis总结_分布式缓存
玩转云端 | 天翼云对象存储ZOS高可用的关键技术揭秘
KunlunBase 1.0 is released!
2022高压电工特种作业证考试题库及答案
cache2go-源码阅读
How a "cloud" can bring about new changes in the industry