MySQL學習足跡記錄03

2022-04-08 19:16:30 字數 4553 閱讀 6762

mysql學習足跡記錄03--order by,desc

1.order by

為了形成對比,這裡先列出不用order by排序的結果

mysql> select prod_name from products; #受mysql重**儲存空間的影響,

#每次查詢排序的結果可能不同

+----------------+

| prod_name |

+----------------+

| .5 ton anvil |

| 1 ton anvil |

| 2 ton anvil |

| detonator |

| bird seed |

| carrots |

| fuses |

| jetpack 1000 |

| jetpack 2000 |

| oil can |

| safe |

| sling |

| tnt (1 stick) |

| tnt (5 sticks) |

+----------------+

*用order by排序

eg:mysql> select prod_name from products order by prod_price;

+----------------+

| prod_name |

+----------------+

| tnt (1 stick) |

| carrots |

| fuses |

| sling |

| .5 ton anvil |

| oil can |

| 1 ton anvil |

| tnt (5 sticks) |

| bird seed |

| detonator |

| 2 ton anvil |

| jetpack 1000 |

| safe |

| jetpack 2000 |

+----------------+

*按多個列排序(先排完a,再從結果中排b)

eg:mysql> select prod_name,prod_id,prod_price from products order by prod_price,prod_name;

#先按prod_price排序,再從排序結果中**相同的部分再按prod_name排序

+----------------+---------+------------+

| prod_name | prod_id | prod_price |

+----------------+---------+------------+

| carrots | fc | 2.50 |

| tnt (1 stick) | tnt1 | 2.50 |

| fuses | fu1 | 3.42 |

| sling | sling | 4.49 |

| .5 ton anvil | anv01 | 5.99 |

| oil can | ol1 | 8.99 |

| 1 ton anvil | anv02 | 9.99 |

| bird seed | fb | 10.00 |

| tnt (5 sticks) | tnt2 | 10.00 |

| detonator | dtntr | 13.00 |

| 2 ton anvil | anv03 | 14.99 |

| jetpack 1000 | jp1000 | 35.00 |

| safe | safe | 50.00 |

| jetpack 2000 | jp2000 | 55.00 |

+----------------+---------+------------+

2.指定排序方向

*預設的排序方向為公升序(asc),為了進行降序,必須用desc關鍵字

eg:mysql> select prod_name,prod_id,prod_price from products order by prod_price desc;

+----------------+---------+------------+

| prod_name | prod_id | prod_price |

+----------------+---------+------------+

| jetpack 2000 | jp2000 | 55.00 |

| safe | safe | 50.00 |

| jetpack 1000 | jp1000 | 35.00 |

| 2 ton anvil | anv03 | 14.99 |

| detonator | dtntr | 13.00 |

| tnt (5 sticks) | tnt2 | 10.00 |

| bird seed | fb | 10.00 |

| 1 ton anvil | anv02 | 9.99 |

| oil can | ol1 | 8.99 |

| .5 ton anvil | anv01 | 5.99 |

| sling | sling | 4.49 |

| fuses | fu1 | 3.42 |

| carrots | fc | 2.50 |

| tnt (1 stick) | tnt1 | 2.50 |

+----------------+---------+------------+

*先降序,再按多個列排序

mysql> select prod_name,prod_id,prod_price from products order by prod_price desc,prod_name;

+----------------+---------+------------+

| prod_name | prod_id | prod_price |

+----------------+---------+------------+

| jetpack 2000 | jp2000 | 55.00 |

| safe | safe | 50.00 |

| jetpack 1000 | jp1000 | 35.00 |

| 2 ton anvil | anv03 | 14.99 |

| detonator | dtntr | 13.00 |

| bird seed | fb | 10.00 |

| tnt (5 sticks) | tnt2 | 10.00 |

| 1 ton anvil | anv02 | 9.99 |

| oil can | ol1 | 8.99 |

| .5 ton anvil | anv01 | 5.99 |

| sling | sling | 4.49 |

| fuses | fu1 | 3.42 |

| carrots | fc | 2.50 |

| tnt (1 stick) | tnt1 | 2.50 |

+----------------+---------+------------+

3.order by和limit的組合

*sql語句是由子句組合成的,有些子句是必須的,而有的是可選的。

mysql> select prod_price from products order by prod_price desc limit 1;

+------------+

| prod_price |

+------------+

| 55.00 |

+------------+

MySQL 學習記錄 03

從這一篇開始就進行資料庫操作的詳細介紹了。總的來說,資料庫的操作大體可分為增刪改查和許可權操作這幾部分。本篇主要分資料庫和表以及查詢簡單說明三部分。檢視資料庫 show databases 建立資料庫 create database db name 建立資料庫並設定字符集為 utf8 create ...

Activiti框架學習記錄 03

本篇主要是activiti工作流框架的學習記錄,以及對於該框架的基本使用和一些淺顯的理解 1 基本概念與名詞理解 2 知識點1 候選人操作 現在的操作因為處於學習階段都是暫時靜態資料 processengine processengine processengines.getdefaultproce...

記錄LaTeX學習過程 03

有了前面的學習,我們對latex有了基本的了解,下來,動手寫一篇小短文,鞏固一下之前所學到的知識點。還是參照耿楠老師的ppt進行學習,寫一篇小短文。documentclass 宣告了標題 author 作者 date 日期 bibliographystyle 宣告參考文獻格式 begin 開始內容 ...