# 排序查询

## 1.仅仅排序

​ `order by`语句一般放在语句的最后面，进行限制

* `ASC`默认升序查询
* `DESC`降序查询

```sql
SELECT 
  * 
FROM
  `pmw_goodstype` 

ORDER BY id ASC;
```

## 2.排序筛选

```sql
SELECT 
  * 
FROM
  `pmw_goodstype` 

 WHERE `parentid` >1

ORDER BY id DESC;
```

按照表达式进行排序

* 通过`salary`进行表达式的计算
* `order By`支持别名

```sql
SELECT 
  id ,salary*12 AS year_salary
FROM
  `pmw_goodstype` 

 WHERE `parentid` >1

ORDER BY year_salary2 DESC;
```

## 3.多个排序条件

* 先出现的排序字段，优先级更高一些；这里当id字段相同时，按照`year_salary`进行降序排序

```sql
SELECT 
  id ,salary*12 AS year_salary
FROM
  `pmw_goodstype` 

 WHERE `parentid` >1

ORDER BY year_salary DESC, id ASC;
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zpliu.gitbook.io/booknote/mysql/cha-xun/02-pai-xu-cha-xun.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
