新建t表
CREATE TABLE `t` (
`id` int NOT NULL AUTO_INCREMENT,
`c` int DEFAULT NULL,
`name` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
存入数据
INSERT INTO `test`.`t` (`id`, `c`, `name`) VALUES (1, 0, 't');
INSERT INTO `test`.`t` (`id`, `c`, `name`) VALUES (2, 1, 't');
INSERT INTO `test`.`t` (`id`, `c`, `name`) VALUES (3, 2, 't');
INSERT INTO `test`.`t` (`id`, `c`, `name`) VALUES (4, 3, 't');
INSERT INTO `test`.`t` (`id`, `c`, `name`) VALUES (5, 4, 't');
c列0表示不做限制,需要将其排在最前面,并按c列由高到低排序,网上查到的大部分都是先按一列指定排序,再按另一列进行排序,与这里的需求不太一样。sql如下
SELECT * FROM `t` order by FIELD(c, 0, c), c desc;
结果为
1 0 t
5 4 t
4 3 t
3 2 t
2 1 t
标签:INSERT,name,INTO,order,field,VALUES,MySQL,test,id
From: https://www.cnblogs.com/burndust/p/18217663