当前位置:网站首页>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,多个列的数据要组合到一起时。
参考:
边栏推荐
猜你喜欢

IDEA相关配置(特别完整)看完此篇就将所有的IDEA的相关配置都配置好了、设置鼠标滚轮修改字体大小、设置鼠标悬浮提示、设置主题、设置窗体及菜单的字体及字体大小、设置编辑区主题、通过插件更换主题

衡量软件产品质量的 14 个指标

Wechat Gymnasium Appointment Mini Program Graduation Design Finished Works (7) Mid-term Inspection Report

LeetCode 2336. 无限集中的最小数字(SortedSet)

危及安全的常见物联网攻击有哪些?

leetcode:622. 设计循环队列【循环队列板子】

我用这一招让团队的开发效率提升了 100%!

golang学习之七:并发编程基础(goroutine、channel、select)

搭建属于自己的知识库(Wikijs)

E-Surfing Cloud 4.0 Distributed Cloud Enables Digital Transformation of Thousands of Industries
随机推荐
C# 术语
Go编译原理系列6(类型检查)
AI+医疗:使用神经网络进行医学影像识别分析
魔豹联盟:佛萨奇2.0dapp系统开发模式详情
影响PoE供电传输距离的除了网线还有啥?
如何减轻企业账户被劫持的攻击?
Gear 月度更新|6 月
Taking advantage of cloud-network integration, e-Surfing Cloud has paved the way for digital transformation for government and enterprises
pydev debugger: warning: trying to add breakpoint to file that does not exist: /tmp/xxx
注释
打补丁的日子,比写代码的日子难熬多了
Wechat Gymnasium Appointment Mini Program Graduation Design Finished Work (5) Task Book
Remember the stuck analysis of an industrial automation control system in .NET
千万级QPS下服务如何才能平滑启动
POE交换机全方位解读(中)
How Tencent architects explained: The principle of Redis high-performance communication (essential version)
下载mysql的源码包
mongodb的游标
2022安全员-C证考试题库模拟考试平台操作
Win11主题下载一直转圈怎么办?Win11主题下载一直转圈的解决方法