1. Introduction When using the 2. Main text First, prepare a SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for user -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'Primary key', `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'User name', `age` int(11) NOT NULL COMMENT 'Age', `sex` smallint(6) NOT NULL COMMENT 'Gender', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 8 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES (1, '李子8', 18, 1); INSERT INTO `user` VALUES (2, '张三', 22, 1); INSERT INTO `user` VALUES (3, '李四', 38, 1); INSERT INTO `user` VALUES (4, '王五', 25, 1); INSERT INTO `user` VALUES (5, '六麻子', 13, 0); INSERT INTO `user` VALUES (6, '田七', 37, 1); INSERT INTO `user` VALUES (7, 'Thank you', 18, 1); SET FOREIGN_KEY_CHECKS = 1; The initial order of the data is as follows: mysql> select * from user; +----+--------+-----+-----+ | id | name | age | sex | +----+--------+-----+-----+ | 1 | Plum 8 | 18 | 1 | | 2 | Zhang San | 22 | 1 | | 3 | Li Si | 38 | 1 | | 4 | Wang Wu | 25 | 1 | | 5 | Liu Mazi | 13 | 0 | | 6 | Tianqi | 37 | 1 | | 7 | Thank you | 18 | 1 | +----+--------+-----+-----+ 7 rows in set (0.00 sec) 2.1 Sorting a single column Let's first look at using need: Sort by user age in ascending order. Statement: select * from user order by age; result: mysql> select * from user order by age; +----+--------+-----+-----+ | id | name | age | sex | +----+--------+-----+-----+ | 5 | Liu Mazi | 13 | 0 | | 1 | Plum 8 | 18 | 1 | | 7 | Thank you | 18 | 1 | | 2 | Zhang San | 22 | 1 | | 4 | Wang Wu | 25 | 1 | | 6 | Tianqi | 37 | 1 | | 3 | Li Si | 38 | 1 | +----+--------+-----+-----+ 7 rows in set (0.00 sec) analyze: You can see that the output order of 2.2 Sorting multiple columns Before testing, let's add a piece of data with the same age to the table. mysql> insert into user (name, age, sex) values ('李子柒', 18, 1); Query OK, 1 row affected (0.01 sec) need: Sort by user age in ascending order and then by user name. Statement: select * from user order by age, name; result: mysql> select * from user order by age, name; +----+--------+-----+-----+ | id | name | age | sex | +----+--------+-----+-----+ | 5 | Liu Mazi | 13 | 0 | | 1 | Plum 8 | 18 | 1 | | 8 | Li Ziqi | 18 | 1 | | 7 | Thank you | 18 | 1 | | 2 | Zhang San | 22 | 1 | | 4 | Wang Wu | 25 | 1 | | 6 | Tianqi | 37 | 1 | | 3 | Li Si | 38 | 1 | +----+--------+-----+-----+ 8 rows in set (0.00 sec) analyze: 2.3 Sorting methodThere are two sorting methods for order by:
mysql> select * from user order by age desc, name desc; +----+--------+-----+-----+ | id | name | age | sex | +----+--------+-----+-----+ | 3 | Li Si | 38 | 1 | | 6 | Tianqi | 37 | 1 | | 4 | Wang Wu | 25 | 1 | | 2 | Zhang San | 22 | 1 | | 7 | Thank you | 18 | 1 | | 8 | Li Ziqi | 18 | 1 | | 1 | Plum 8 | 18 | 1 | | 5 | Liu Mazi | 13 | 0 | +----+--------+-----+-----+ 8 rows in set (0.00 sec) If you only specify descending sorting of mysql> select * from user order by age desc, name; +----+--------+-----+-----+ | id | name | age | sex | +----+--------+-----+-----+ | 3 | Li Si | 38 | 1 | | 6 | Tianqi | 37 | 1 | | 4 | Wang Wu | 25 | 1 | | 2 | Zhang San | 22 | 1 | | 1 | Plum 8 | 18 | 1 | | 8 | Li Ziqi | 18 | 1 | | 7 | Thank you | 18 | 1 | | 5 | Liu Mazi | 13 | 0 | +----+--------+-----+-----+ 8 rows in set (0.00 sec) It can be seen that the sorting method of the data in the three rows of Li Ziba, Li Ziqi and Xie Li has changed. 2.4 order by combined with limit mysql> select * from user order by age desc limit 1; +----+------+-----+-----+ | id | name | age | sex | +----+------+-----+-----+ | 3 | Li Si | 38 | 1 | +----+------+-----+-----+ 1 row in set (0.00 sec)
mysql> select * from user limit 1 order by age des; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order by age des' at line 1 This is the end of this article about the details of using order by in MySQL. For more information about the use of order by in MySQL, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Share 20 JavaScript one-line codes
>>: Method of building docker private warehouse based on Harbor
This article uses examples to explain the concept...
Function: Jump to the previous page or the next p...
①. How to use the alias (CNAME) record: In the do...
Installation path: /application/mysql-5.7.18 1. P...
Table of contents Introduction Traditional transi...
This article example shares the specific code of ...
Problem Description After installing Qt5.15.0, an...
What should I do if Linux does not support all co...
Today we will introduce how to publish the local ...
This article example shares the specific code of ...
When configuring proxy_pass in nginx, if you matc...
## 1 I'm learning docker deployment recently,...
Docker is an open source container engine that he...
Error message: ERROR 2002: Can't connect to l...
Introduction Recently, I needed to make a barrage...