【Mysql】教程全解(三)ORDERBY排序3.1 order by
当使⽤SELECT语句查询表中的数据时,结果集不按任何顺序进⾏排序。要对结果集进⾏排
序,请使⽤ORDER BY⼦句。
ORDER BY⼦句允许:
对单个列或多个列排序结果集。
按升序或降序对不同列的结果集进⾏排序。
下⾯说明了ORDER BY⼦句的语法:
SELECT column1, column2,...
FROM tbl
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC],...
SQL
ASC表⽰升序,DESC表⽰降序。默认情况下,如果不明确指定ASC或DESC,ORDER BY⼦句会按
照升序对结果集进⾏排序。
下⾯我们来学习和练习⼀些使⽤ORDER BY⼦句的例⼦。
2. MySQL ORDER BY⽰例
请参见⽰例数据库(yiibaidb)中的customers表,customers表的结构如下所⽰ -
以下查询从customers表中查询联系⼈,并按contactLastname升序对联系⼈进⾏排序。
SELECT contactLastname, contactFirstname
FROM customers
ORDER BY contactLastname;
SQL
执⾏上⾯查询,得到以下结果 -
mysql> SELECT contactLastname, contactFirstname FROM customers ORDER BY contactLastname; ----------------------------------- | contactLastname | contactFirstname | ----------------- ------------------ | Accorti | Paolo | | Altagar,G M | Raanan | | Andern | Mel | | Anton | Carmen | | Ashworth | Rachel | | Barajas | Miguel | | Benitez | Violeta | | Bennett | Helen | | Berglund | Christina | | Bergulfn | Jonas | | Bertrand | Marie | ... .... | Young | Julie | | Young | Mary | | Young | Dorothy | ----------------- ------------------ 122 rows in t
Shell
如果要按姓⽒降序对联系⼈进⾏排序,请在ORDER BY⼦句中的contactLastname列后⾯指定DESC,如下查询:
SELECT contactLastname, contactFirstname
FROM customers
ORDER BY contactLastname DESC;
什么字五行属土SQL
执⾏上⾯查询,得到以下结果 -
mysql> SELECT contactLastname, contactFirstname FROM customers ORDER BY contactLastname DESC; ----------------- ------------------ | contactLastname | contactFirstname | ----------------- ------------------ | Young | Jeff | | Young | Julie | | Young | Mary | ... ... | Anton | Carmen | | Andern | Mel | | Altagar,G M | Raanan | | Accorti | Paolo | ----------------- ------------------ 122 rows in t
Shell
如果要按姓⽒按降序和名字按升序排序联系⼈,请在相应列中分别指定DESC和ASC,如下所⽰:
SELECT contactLastname, contactFirstname
FROM customers
ORDER BY contactLastname DESC, contactFirstname ASC;
SQL
执⾏上⾯查询,得到以下结果 -
mysql> SELECT contactLastname, contactFirstname FROM customers ORDER BY contactLastname DESC, contactFirstname ASC; ----------------- ------------------ | contactLastname | contactFirstname | ----------------- ------------------ | Young | Dorothy | | Young | Jeff | | Young | Julie | | Young | Mary | | Yoshido | Juri | | Walker | Brydey | | Victorino | Wendy | | Urs | Braun | | Tng | Jerry | ... ... | Brown | Julie | | Brown | William | | Bertrand | Marie | |
Bergulfn | Jonas | | Berglund | Christina | | Bennett | Helen | | Benitez | Violeta | | Barajas | Miguel | | Ashworth | Rachel | | Anton | Carmen | | Andern | Mel | | Altagar,G M | Raanan | | Accorti | Paolo | ----------------- ------------------ 122 rows in t
Shell
在上⾯的查询中,ORDER BY⼦句⾸先按照contactLastname列降序对结果集进⾏排序,然后按
照contactFirstname列升序对排序结果集进⾏排序,以⽣成最终结果集。
MySQL ORDER BY按表达式排序⽰例
ORDER BY⼦句还允许您根据表达式对结果集进⾏排序。请参阅以下orderdetails表结构 -
以下查询从orderdetails表中选择订单⾏记录项⽬。它计算每个订单项的⼩计,并根据订单编号,订单⾏号(orderLineNumber)和⼩计(quantityOrdered * priceEach)对结果集进⾏排序。
SELECT ordernumber, orderlinenumber, quantityOrdered * priceEach
FROM orderdetails
会动的玩具ORDER BY ordernumber, orderLineNumber, quantityOrdered * priceEach;
SQL
执⾏上⾯语句,总共有2996⾏结果集,以下是部分结果集⽚断 -
为了使查询更易于阅读,可以按列别名进⾏排序,⽅法如下:
SELECT ordernumber, orderlinenumber, quantityOrdered * priceEach AS subtotal
FROM orderdetails
ORDER BY ordernumber, orderLineNumber, subtotal;
SQL
执⾏上⾯语句,总共有2996⾏结果集,以下是部分结果集⽚断 -
上⾯表达式中,使⽤subtotal作为表达式quantityOrdered * priceEach的列别名,并根据⼩计别名(subtotal)对结果集进⾏排序。
MySQL ORDER BY与⾃定义排序顺序
ORDER BY⼦句允许使⽤FIELD()函数为列中的值定义⾃⼰的⾃定义排序顺序。
看看下⾯orders表的结构如下所⽰ -
例如,如果要按以下顺序基于以下状态的值对订单进⾏排序:
海锚
In Process
On Hold守望明天
赵信改版Cancelled
Resolved
Disputed
Shipped
什么叫单反相机
可以使⽤FIELD()函数将这些值映射到数值列表,并使⽤数字进⾏排序; 请参阅以下查询:
SELECT orderNumber, status
FROM orders
ORDER BY FIELD(status, 'In Process', 'On Hold', 'Cancelled', 'Resolved', 'Disputed',
'Shipped');
SQL
执⾏上⾯查询语句,得到以下结果 -
mysql> SELECT orderNumber, status FROM orders ORDER BY FIELD(status, 'In Process', 'On Hold', 'Cancelled', 'Resolved', 'Disputed', 'Shipped'); ------------- ------------ | orderNumber | status | ------------- ------------ | 10420 | In Process | | 10421 | In Process | | 10422 | In Process | | 10423 | In Process | | 10424 | In Process | | 10425 | In Process | | 10334 | On Hold | | 10401 | On Hold | | 10407 | On Hold | | 10414 | On Hold | | 10167 | Cancelled | | 10179 | Cancelled | | 10248 | Cancelled | | 10253 | Cancelled | | 10260 | Cancelled | | 10262 | Cancelled | | 10164 | Resolved | | 10327 | Resolved | ... ... | 10413 | Shipped | | 10416 | Shipped | | 10418 | Shipped | | 10419 | Shipped | ------------- ------------ 326 rows in t
3 MySQL GROUP BY与聚合函数
可使⽤聚合函数来执⾏⼀组⾏的计算并返回单个值。GROUP BY⼦句通常与聚合函数⼀起使⽤以执⾏计算每个分组并返回单个值。
例如,如果想知道每个状态中的订单数,可以使⽤COUNT函数与GROUP BY⼦句查询语句,如下所⽰:
SELECT status, COUNT(*) AS total_number
FROM orders
GROUP BY status;
SQL
爱情说执⾏上⾯查询语句,得到以下结果 -
几月份供暖
要按状态获取所有订单的总⾦额,可以使⽤orderdetails表连接orders表,并使⽤SUM函数计算总⾦额。请参阅以下查询:
SELECT status, SUM(quantityOrdered * priceEach) AS amount
FROM orders
INNER JOIN orderdetails USING (orderNumber)
GROUP BY status;
SQL
执⾏上⾯查询,得到以下结果 -
类似地,以下查询返回订单号和每个订单的总⾦额。
SELECT orderNumber, SUM(quantityOrdered * priceEach) AS total
FROM orderdetails
GROUP BY orderNumber;
SQL
执⾏上⾯查询,得到以下结果 -
2.3 MySQL GROUP BY⽤表达式⽰例
除了列之外,可以按表达式对⾏进⾏分组。以下查询获取每年的总销售额。
SELECT YEAR(orderDate) AS year, SUM(quantityOrdered * priceEach) AS total
FROM orders
INNER JOIN orderdetails USING (orderNumber)
WHERE status = 'Shipped'
GROUP BY YEAR(orderDate);
SQL
执⾏上⾯查询,得到以下结果 -
在这个例⼦中,我们使⽤YEAR函数从订单⽇期(orderDate)中提取年份数据。只包括已发货