当前位置:网站首页>查询进阶 别名
查询进阶 别名
2022-07-26 11:09:00 【华为云】
13.8 使用别名查询数据
MySQL支持在查询数据时为字段名或表名指定别名,指定别名时可以使用AS关键字,也可以不使用。
13.8.1 为字段名指定别名
为字段名指定别名的语法格式如下:
SELECT column1 [AS] col1 [, column2 [AS] col2, … , columnn [AS] coln] FROM table_name其中,AS关键字可以省略。
查询t_goods数据表中t_category_id和t_category字段的数据。
mysql> SELECT t_category_id categoryId, t_category AS categoryName FROM t_goods;+---------------+----------------+| t_categoryId | categoryName |+---------------+----------------+| 1 | 女装/女士精品 || 1 | 女装/女士精品 || 1 | 女装/女士精品 || 1 | 女装/女士精品 || 1 | 女装/女士精品 || 1 | 女装/女士精品 || 2 | 户外运动 || 2 | 户外运动 || 2 | 户外运动 || 2 | 户外运动 || 2 | 户外运动 || 2 | 户外运动 |+---------------+----------------+12 rows in set (0.00 sec)结果显示正确地查询出了数据。由此可见,使用AS关键字和不使用AS关键字都能为字段指定别名。当为字段指定别名时,查询的结果数据列表中的字段名称显示的是别名;否则显示的是字段名称。
13.8.2 为表名指定别名
为表名指定别名的语法格式如下:
SELECT * FROM table_name [AS] tableName或者
SELECT column1 [, column2, … , columnn] FROM table_name [AS] tableName其中,AS关键字可以省略。
通过为数据表指定别名的方式查询t_goods数据表中t_name字段和t_stock字段的数据。
mysql> SELECT t_name, t_stock FROM t_goods goods;+--------------+---------+| t_name | t_stock |+--------------+---------+| T恤 | 1000 || 连衣裙 | 2500 || 卫衣 | 1500 || 牛仔裤 | 3500 || 百褶裙 | 500 || 呢绒外套 | 1200 || 自行车 | 1000 || 山地自行车 | 2500 || 登山杖 | 1500 || 骑行装备 | 3500 || 户外运动外套 | 500 || 滑板 | 1200 |+--------------+---------+12 rows in set (0.00 sec)13.8.3 同时为字段名和表名指定别名
同时为字段名和表名指定别名的语法格式如下:
SELECT column1 [AS] col1 [, column2 [AS] col2, … , columnn [AS] coln] FROM table_name [AS] tableName其中,AS关键字可以省略。
查询t_goods数据表中t_name字段、t_price字段和t_upper_time字段的数据。
mysql> SELECT -> t_name name, t_price AS price, t_upper_time upperTime -> FROM t_goods AS goods;+-------------+---------+---------------------+| name | price | uppertime |+-------------+---------+---------------------+| T恤 | 39.90 | 2020-11-10 00:00:00 || 连衣裙 | 79.90 | 2020-11-10 00:00:00 || 卫衣 | 79.90 | 2020-11-10 00:00:00 || 牛仔裤 | 89.90 | 2020-11-10 00:00:00 || 百褶裙 | 29.90 | 2020-11-10 00:00:00 || 呢绒外套 | 399.90 | 2020-11-10 00:00:00 || 自行车 | 399.90 | 2020-11-10 00:00:00 || 山地自行车 | 1399.90 | 2020-11-10 00:00:00 || 登山杖 | 59.90 | 2020-11-10 00:00:00 || 骑行装备 | 399.90 | 2020-11-10 00:00:00 || 户外运动外套 | 799.90 | 2020-11-10 00:00:00 || 滑板 | 499.90 | 2020-11-10 00:00:00 |+-------------+---------+---------------------+12 rows in set (0.00 sec)注意:为字段或表指定别名,在多表关联查询时使用较多。
边栏推荐
猜你喜欢
随机推荐
HCI interface
easyui03
[vscode]如何远程连接服务器
Scrapy IP agent is not responding
Ten year structure five year life-06 impulse to leave
The combination of pytest confitest.py and fixture
MySQL transaction details
为什么放弃npm转向yarn了
The company cannot access station B
[vscode] how to connect to the server remotely
easyui05
《微信小程序-进阶篇》Lin-ui组件库源码分析-Button组件(一)
ArrayList of novice source code
常用库安装
mysql数据库进阶
Mysql database advanced
Record errors encountered by individuals
[learning progress] may
一步一步入门使用g2o解决ICP问题-估计有匹配关系的两组3维点集之间的变换关系
线程之间的几种通信方式









