Home > Database > Mysql Tutorial > MySQL中select语句使用order按行排序_MySQL

MySQL中select语句使用order按行排序_MySQL

WBOY
Release: 2016-05-27 13:44:35
Original
1402 people have browsed it

本文介绍MySQL数据库中执行select查询语句,并对查询的结果使用order by 子句进行排序。

再来回顾一下SQL语句中的select语句的语法:

Select 语句的基本语法:

Select from where order by

如果要对查询结果按某个字段排序,则要使用order by 子句,如下:

select * from order by

下面来看两个例子,第一个查询test表中所有数据,并按t_id正序排列;第二个查询与第一个相反,是逆序排列。

mysql> select t_id,t_name from test order by t_id;
+------+--------+
| t_id | t_name |
+------+--------+
|    1 | name1  |
|    2 | name2  |
+------+--------+
2 rows in set (0.00 sec)

mysql> select t_id,t_name from test order by t_id desc;
+------+--------+
| t_id | t_name |
+------+--------+
|    2 | name2  |
|    1 | name1  |
+------+--------+
2 rows in set (0.00 sec)

注意:正序排序时,可以使用asc作为排序方式的关键词,也可以不使用。逆序时,一定要使用desc作为关键词。

关于MySQL中select语句使用order按行排序,本文就介绍这么多,希望对大家有所帮助,谢谢!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template