测试sql数据
CREATE TABLE `student` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
`age` int DEFAULT NULL,
`money` int DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE,
KEY `index_name` (`age`)
);
INSERT INTO `student` VALUES (1, '张三', 22, 1000);
INSERT INTO `student` VALUES (2, '李四', 23, 1000);
INSERT INTO `student` VALUES (3, '王五', 24, 2000);
INSERT INTO `student` VALUES (4, '赵六', 10, 1500);
INSERT INTO `student` VALUES (5, '孙七', 21, 2000);
INSERT INTO `student` VALUES (6, '周八', 30, 1200);
INSERT INTO `student` VALUES (7, '吴九', 21, 1100);
INSERT INTO `student` VALUES (8, '郑十', 21, 1400);
INSERT INTO `student` VALUES (9, '张三哥哥', 23, 1231);
INSERT INTO `student` VALUES (10, '李四姐姐', 25, 1000);
INSERT INTO `student` VALUES (11, '1张三', 21, 1000);
INSERT INTO `student` VALUES (12, '2张三', 21, 1000);
INSERT INTO `student` VALUES (13, '11张三', 23, 1000);
问题一: 请按拥有的money进行从大到小排序
问题二: 请按拥有的money进行倒序,相同money的情况下以年龄从小到大排序
问题三: 按姓名拼音排序
问题四: 如果名字开头中含有数字,请按数字从大到小排序,开头不含数字的名字按拼音排序
问题五: 按 张三、李四、王五的顺序进行排序,剩下以上述的规则排序
参考答案:
-- 请按拥有的money进行倒序
select * from student order by money desc;
-- 请按拥有的money进行倒序,相同money的情况下以年龄从小到大排序
select * from student order by money desc, age;
-- 按 姓名拼音排序
select * from student order by CONVERT(name USING gbk);
-- 如果名字开头中含有数字,请按数字从大到小排序,开头不含数字的名字按拼音排序
select * from student order by IF(name REGEXP '^[0-9]', 1, 2),
IF(name REGEXP '^[0-9]', CAST(name AS SIGNED), 0) DESC,
CONVERT(name USING gbk);
-- 按 张三、李四、王五的顺序进行排序,其他数据按money进行倒序,相同money的情况下以年龄从小到大排序
select * from student order by
CASE name
WHEN '张三' THEN 1
WHEN '李四' THEN 2
WHEN '王五' THEN 3
ELSE 4
END, money desc, age;