第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

MySQL ORDER BY

標(biāo)簽:
MySQL

Summary: in this tutorial, you will learn how to sort a result set by using MySQL ORDER BY clause.

Introduction to MySQL ORDER BY clause

When you use the SELECT statement to query data from a table, the result set is not sorted in a specific order. To sort the result set, you use the ORDER BY clause. The ORDER BY clause allows you to:

  • Sort a result set by a single column or multiple columns.

  • Sort a result set by different columns in ascending or descending order.

The following illustrates the syntax of the ORDER BY clause:

SELECT col1, col2,... FROM tbl ORDER BY col1 [ASC|DESC], col2 [ASC|DESC],...

The ASC stands for ascending and the DESC stands for descending. By default, the ORDER BY clause sorts the result set in ascending order  if you don’t  specify ASC or DESC explicitly

Let’s practice with some examples of using the ORDER BY clause.

MySQL ORDER BY examples

The following query selects contacts from the customers table and sorts the contacts by last name in ascending order.

SELECT contactLastname,         contactFirstname FROM customers ORDER BY contactLastname;

mysql order by asc

If you want to sort the contact by last name in descending order, you specify the DESC after the contactLastnamecolumn in the ORDER BY clause as the following query:

SELECT contactLastname,         contactFirstname FROM customers ORDER BY contactLastname DESC

mysql order by desc

If you want to sort the contacts by last name in descending order and first name in ascending order, you specify both  DESC and ASC in the corresponding column as follows:

SELECT contactLastname,         contactFirstname FROM customers ORDER BY contactLastname DESC,           contactFirstname ASC;

mysql order by desc asc

In the query above, the ORDER BY clause sorts the result set by  last name in descending order first, and then sorts the sorted result set by first name in ascending order to produce the final result set.

MySQL ORDER BY sort by an expression example

The ORDER BY clause also allows you to sort the result set based on an expression. The following query selects the order line items from the orderdetails table. It calculates the subtotal for each line item and sorts the result set based on the order number and subtotal.

SELECT ordernumber,         quantityOrdered * priceEach  FROM orderdetails ORDER BY ordernumber,           quantityOrdered * priceEach

mysql order by sort by expression

To make the result more readable, you can use a column alias, and sort the result based on the column alias.

SELECT orderNumber,         quantityOrdered * priceEach AS subTotal FROM orderdetails ORDER BY orderNumber,           subTotal;

 

1

 

mysql order by sort by column alias

In the query above, we used subtotal as the column alias for the quantityOrdered * priceEach expression and sorted the result set based on the subtotal alias.

If you use  a function that returns a value whose data type is different from the column’s and sort the result based on the alias, the ORDER BY clause will sort the result set based on the return type of the function, which may not work as expected.

For example, if you use the DATE_FORMAT function to format the date values and sort the result set based on the strings returned by the DATE_FORMAT function, the order is not always correct. For more information, check it out the example in the DATE_FORMAT function tutorial.

MySQL ORDER BY with customer sort order

The ORDER BY clause enables you to define your own custom sort order for the values in a column using the FIELD() function. For example, if you want to sort the orders based on the following status by the following order:

  1. In Process

  2. On Hold

  3. Cancelled

  4. Resolved

  5. Disputed

  6. Shipped

You can use the FIELD() function to map those values to a list of numeric values and use the numbers for sorting; See the following query:

 https://img1.sycdn.imooc.com//5b65847200012c4101520051.jpg


點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消