在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1、简介在使用 2、正文首先准备一张 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 '主键', `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '用户名', `age` int(11) NOT NULL COMMENT '年龄', `sex` smallint(6) NOT NULL COMMENT '性别', 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, '李子捌', 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, '谢礼', 18, 1); SET FOREIGN_KEY_CHECKS = 1; 数据的初始顺序如下所示: mysql> select * from user; +----+--------+-----+-----+ | id | name | age | sex | +----+--------+-----+-----+ | 1 | 李子捌 | 18 | 1 | | 2 | 张三 | 22 | 1 | | 3 | 李四 | 38 | 1 | | 4 | 王五 | 25 | 1 | | 5 | 六麻子 | 13 | 0 | | 6 | 田七 | 37 | 1 | | 7 | 谢礼 | 18 | 1 | +----+--------+-----+-----+ 7 rows in set (0.00 sec) 2.1 单个列排序我们首先来看使用 需求: 根据用户年龄进行升序排序。 语句: select * from user order by age; 结果: mysql> select * from user order by age; +----+--------+-----+-----+ | id | name | age | sex | +----+--------+-----+-----+ | 5 | 六麻子 | 13 | 0 | | 1 | 李子捌 | 18 | 1 | | 7 | 谢礼 | 18 | 1 | | 2 | 张三 | 22 | 1 | | 4 | 王五 | 25 | 1 | | 6 | 田七 | 37 | 1 | | 3 | 李四 | 38 | 1 | +----+--------+-----+-----+ 7 rows in set (0.00 sec) 分析: 可以看到 2.2 多个列排序
在测试之前,我们先往表中添加一条年龄相等的数据 mysql> insert into user (name, age, sex) values ('李子柒', 18, 1); Query OK, 1 row affected (0.01 sec) 需求: 根据用户年龄升序排序之后再更加用户名称排序。 语句: select * from user order by age, name; 结果: mysql> select * from user order by age, name; +----+--------+-----+-----+ | id | name | age | sex | +----+--------+-----+-----+ | 5 | 六麻子 | 13 | 0 | | 1 | 李子捌 | 18 | 1 | | 8 | 李子柒 | 18 | 1 | | 7 | 谢礼 | 18 | 1 | | 2 | 张三 | 22 | 1 | | 4 | 王五 | 25 | 1 | | 6 | 田七 | 37 | 1 | | 3 | 李四 | 38 | 1 | +----+--------+-----+-----+ 8 rows in set (0.00 sec) 分析:
2.3 排序的方式order by有两种排序方式,它们分别是:
mysql> select * from user order by age desc, name desc; +----+--------+-----+-----+ | id | name | age | sex | +----+--------+-----+-----+ | 3 | 李四 | 38 | 1 | | 6 | 田七 | 37 | 1 | | 4 | 王五 | 25 | 1 | | 2 | 张三 | 22 | 1 | | 7 | 谢礼 | 18 | 1 | | 8 | 李子柒 | 18 | 1 | | 1 | 李子捌 | 18 | 1 | | 5 | 六麻子 | 13 | 0 | +----+--------+-----+-----+ 8 rows in set (0.00 sec) 如果你只指定 mysql> select * from user order by age desc, name; +----+--------+-----+-----+ | id | name | age | sex | +----+--------+-----+-----+ | 3 | 李四 | 38 | 1 | | 6 | 田七 | 37 | 1 | | 4 | 王五 | 25 | 1 | | 2 | 张三 | 22 | 1 | | 1 | 李子捌 | 18 | 1 | | 8 | 李子柒 | 18 | 1 | | 7 | 谢礼 | 18 | 1 | | 5 | 六麻子 | 13 | 0 | +----+--------+-----+-----+ 8 rows in set (0.00 sec) 可以看到李子捌、李子柒、谢礼三行数据排序方式发生了改变。 2.4 order by结合limit
mysql> select * from user order by age desc limit 1; +----+------+-----+-----+ | id | name | age | sex | +----+------+-----+-----+ | 3 | 李四 | 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 到此这篇关于MySQL中order by的使用详情的文章就介绍到这了,更多相关MySQL中的order by使用内容请搜索极客世界以前的文章或继续浏览下面的相关文章希望大家以后多多支持极客世界! |
请发表评论