order by a,b desc 和 order by a desc,b desc 区别
order by a,b desc
select * from demo order by create_time,id desc
MySql中,order by 默认是按升序排列的。 显然,不写排序方式,默认是升序。
order by a desc,b desc
select * from demo order by create_time desc,id desc
DROP TABLE IF EXISTS `demo`;
CREATE TABLE `demo` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`create_time` datetime(0) DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 11 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `demo` VALUES (1, '11', '2021-11-04 10:33:35');
INSERT INTO `demo` VALUES (3, '11', '2021-11-04 10:33:36');
INSERT INTO `demo` VALUES (4, '11', '2021-11-04 10:33:37');
INSERT INTO `demo` VALUES (5, '11', '2021-11-04 10:33:38');
INSERT INTO `demo` VALUES (6, '11', '2021-11-04 10:33:35');
INSERT INTO `demo` VALUES (7, '11', '2021-11-04 10:33:35');
INSERT INTO `demo` VALUES (8, '11', '2021-11-04 10:33:35');
INSERT INTO `demo` VALUES (9, '11', '2021-11-04 10:33:35');
INSERT INTO `demo` VALUES (10, '11', '2021-11-04 10:33:35');
总结
(1)在MySql中,使用 ASC 或 DESC 关键字来设置查询结果是按升序或降序排列。
(2)尤其非常特别重要:默认按升序(ASC)排列。
(3)order by 后可加2个(或多个)字段,字段之间用英文逗号隔开。
(4)若A用升序,B用降序,SQL该这样写:order by A ASC, B DESC; 默认同理,也可以这样写:order by A, B DESC;
(5)若A、B都用降序,必须用两个DESC,order by A DESC, B DESC;
(6)多个字段时,优先级按先后顺序而定。