mysql中as怎么用

52次阅读
没有评论

共计 5215 个字符,预计需要花费 14 分钟才能阅读完成。

本篇内容主要讲解“mysql 中 as 怎么用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让丸趣 TV 小编来带大家学习“mysql 中 as 怎么用”吧!

在 mysql 中,“as”关键字用于为数据表和字段指定别名,语法:1、“SELECT 字段名 AS 别名 FROM 数据表;”,可为字段指定别名;2、“SELECT 字段名 FROM 数据表 AS 别名;”,可为表指定别名。

本教程操作环境:windows7 系统、mysql8 版本、Dell G3 电脑。

为了查询方便,MySQL 提供了 AS 关键字来为表和字段指定别名。本节主要讲解如何为表和字段指定一个别名。

在使用 MySQL 查询时,当表名很长或者执行一些特殊查询的时候,为了方便操作或者需要多次使用相同的表时,可以为表指定别名,用这个别名代替表原来的名称。

为字段指定别名

有时,列的名称是一些表达式,使查询的输出很难理解。要给列一个描述性名称,可以使用列别名。

以下语句说明了如何使用列别名:

SELECT 
  字段名  AS  别名
FROM  数据表;

要给字段添加别名,可以使用 AS 关键词后跟别名。如果别名包含空格,则必须引用以下内容:

SELECT 
  字段名  AS ` 别名 `
FROM  数据表;

因为 AS 关键字是可选的,可以在语句中省略它。请注意,还可以在表达式上使用别名。

我们来看看示例数据库中的 employees 表,其表结构如下所示 –

mysql  desc employees;
+----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+-------+
| employeeNumber | int(11) | NO | PRI | NULL | |
| lastName | varchar(50) | NO | | NULL | |
| firstName | varchar(50) | NO | | NULL | |
| extension | varchar(10) | NO | | NULL | |
| email | varchar(100) | NO | | NULL | |
| officeCode | varchar(10) | NO | MUL | NULL | |
| reportsTo | int(11) | YES | MUL | NULL | |
| jobTitle | varchar(50) | NO | | NULL | |
+----------------+--------------+------+-----+---------+-------+
8 rows in set

以下查询选择员工的名字和姓氏,并将其组合起来生成全名。CONCAT_WS 函数用于连接名字和姓氏。

SELECT 
 CONCAT_WS(,  , lastName, firstname)
 employees;

执行上面代码,得到以下结果 –

mysql  SELECT 
 CONCAT_WS(,  , lastName, firstname)
 employees;
+--------------------------------------+
| CONCAT_WS(,  , lastName, firstname) |
+--------------------------------------+
| Murphy, Diane |
| Patterson, Mary |
| Firrelli, Jeff |
| Patterson, William |
| Bondur, Gerard |
| Bow, Anthony |
| Jennings, Leslie |
| Thompson, Leslie |
| Firrelli, Julie |
| Patterson, Steve |
| Tseng, Foon Yue |
| Vanauf, George |
| Bondur, Loui |
| Hernandez, Gerard |
| Castillo, Pamela |
| Bott, Larry |
| Jones, Barry |
| Fixter, Andy |
| Marsh, Peter |
| King, Tom |
| Nishi, Mami |
| Kato, Yoshimi |
| Gerard, Martin |
+--------------------------------------+
23 rows in set

在上面示例中,列标题很难阅读理解。可以为输出的标题分配一个有意义的列别名,以使其更可读,如以下查询:

SELECT
 CONCAT_WS(,  , lastName, firstname) AS `Full name`
 employees;

执行上面代码,得到以下结果 –

mysql  SELECT
 CONCAT_WS(,  , lastName, firstname) AS `Full name`
 employees;
+--------------------+
| Full name |
+--------------------+
| Murphy, Diane |
| Patterson, Mary |
| Firrelli, Jeff |
... ...
| King, Tom |
| Nishi, Mami |
| Kato, Yoshimi |
| Gerard, Martin |
+--------------------+
23 rows in set

在 MySQL 中,可以使用 ORDER BY,GROUP BY 和 HAVING 子句中的列别名来引用该列。

以下查询使用 ORDER BY 子句中的列别名按字母顺序排列员工的全名:

SELECT
 CONCAT_WS(  , lastName, firstname) `Full name`
 employees
ORDER BY
 `Full name`;

执行上面代码,得到以下结果 –

mysql  SELECT
 CONCAT_WS(  , lastName, firstname) `Full name`
 employees
ORDER BY
 `Full name`;
+-------------------+
| Full name |
+-------------------+
| Bondur Gerard |
| Bondur Loui |
| Bott Larry |
| Bow Anthony |
| Castillo Pamela |
| Firrelli Jeff |
| Firrelli Julie |
| Fixter Andy |
| Gerard Martin |
| Hernandez Gerard |
| Jennings Leslie |
| Jones Barry |
| Kato Yoshimi |
| King Tom |
| Marsh Peter |
| Murphy Diane |
| Nishi Mami |
| Patterson Mary |
| Patterson Steve |
| Patterson William |
| Thompson Leslie |
| Tseng Foon Yue |
| Vanauf George |
+-------------------+
23 rows in set

以下语句查询总金额大于 60000 的订单。它在 GROUP BY 和 HAVING 子句中使用列别名。

SELECT
 orderNumber `Order no.`,
 SUM(priceEach * quantityOrdered) total
 orderdetails
GROUP BY
 `Order no.`
HAVING
 total   60000;

执行上面查询语句,得到以下结果 –

mysql  SELECT
 orderNumber `Order no.`,
 SUM(priceEach * quantityOrdered) total
 orderdetails
GROUP BY
 `Order no.`
HAVING
 total   60000;
+-----------+----------+
| Order no. | total |
+-----------+----------+
| 10165 | 67392.85 |
| 10287 | 61402.00 |
| 10310 | 61234.67 |
+-----------+----------+
3 rows in set

请注意,不能在 WHERE 子句中使用列别名。原因是当 MySQL 评估求值 WHERE 子句时,SELECT 子句中指定的列的值可能尚未确定。

为表指定别名

可以使用别名为表添加不同的名称。使用 AS 关键字在表名称分配别名,如下查询语句语法:

SELECT  字段名  FROM  数据表  AS  别名;

该表的别名称为表别名。像列别名一样,AS 关键字是可选的,所以完全可以省略它。

一般在包含 INNER JOIN,LEFT JOIN,self join 子句和子查询的语句中使用表别名。

下面来看看客户 (customers) 和订单 (orders) 表,它们的 ER 图如下所示 –

两个表都具有相同的列名称:customerNumber。如果不使用表别名来指定是哪个表中的 customerNumber 列,则执行查询时将收到类似以下错误消息:

Error Code: 1052. Column  customerNumber  in on clause is ambiguous

为避免此错误,应该使用表别名来限定 customerNumber 列:

SELECT
 customerName,
 COUNT(o.orderNumber) total
 customers c
INNER JOIN orders o ON c.customerNumber = o.customerNumber
GROUP BY
 customerName
HAVING total  =5
ORDER BY
 total DESC;

执行上面查询语句,得到以下结果 –

mysql  SELECT
 customerName,
 COUNT(o.orderNumber) total
 customers c
INNER JOIN orders o ON c.customerNumber = o.customerNumber
GROUP BY
 customerName
HAVING total  =5
ORDER BY
 total DESC;
+------------------------------+-------+
| customerName | total |
+------------------------------+-------+
| Euro+ Shopping Channel | 26 |
| Mini Gifts Distributors Ltd. | 17 |
| Reims Collectables | 5 |
| Down Under Souveniers, Inc | 5 |
| Danish Wholesale Imports | 5 |
| Australian Collectors, Co. | 5 |
| Dragon Souveniers, Ltd. | 5 |
+------------------------------+-------+
7 rows in set

上面的查询从客户 (customers) 和订单 (orders) 表中选择客户名称和订单数量。它使用 c 作为 customers 表的表别名,o 作为 orders 表的表别名。customers 和 orders 表中的列通过表别名 (c 和 o) 引用。

如果您不在上述查询中使用别名,则必须使用表名称来引用其列,这样的会使得查询冗长且可读性较低,如下 –

SELECT
 customers.customerName,
 COUNT(orders.orderNumber) total
 customers
INNER JOIN orders ON customers.customerNumber = orders.customerNumber
GROUP BY
 customerName
ORDER BY
 total DESC

到此,相信大家对“mysql 中 as 怎么用”有了更深的了解,不妨来实际操作一番吧!这里是丸趣 TV 网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-15发表,共计5215字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)