SQL151. 注册时间最早的三个人
描述
现有用户信息表user_info(uid用户ID,nick_name昵称, achievement成就值, level等级, job职业方向, register_time注册时间):id | uid | nick_name | achievement | level | job | register_time |
1 | 1001 | 牛客1号 | 19 | 0 | 算法 | 2020-01-01 10:00:00 |
2 | 1002 | 牛客2号 | 1200 | 3 | 算法 | 2020-02-01 10:00:00 |
3 | 1003 | 牛客3号♂ | 22 | 0 | 算法 | 2020-01-02 10:00:00 |
4 | 1004 | 牛客4号 | 25 | 0 | 算法 | 2020-01-02 11:00:00 |
5 | 1005 | 牛客555号 | 4000 | 7 | C++ | 2020-01-11 10:00:00 |
6 | 1006 | 666666 | 3000 | 6 | C++ | 2020-11-01 10:00:00 |
uid | nick_name | register_time |
1001 | 牛客1 | 2020-01-01 10:00:00 |
1003 | 牛客3号♂ | 2020-01-02 10:00:00 |
1004 | 牛客4号 | 2020-01-02 11:00:00 |
示例1
输入:
drop table if exists user_info,exam_record; CREATE TABLE user_info ( id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID', uid int UNIQUE NOT NULL COMMENT '用户ID', `nick_name` varchar(64) COMMENT '昵称', achievement int COMMENT '成就值', level int COMMENT '用户等级', job varchar(32) COMMENT '职业方向', register_time datetime COMMENT '注册时间' )CHARACTER SET utf8 COLLATE utf8_general_ci; INSERT INTO user_info(uid,`nick_name`,achievement,level,job,register_time) VALUES (1001, '牛客1', 19, 0, '算法', '2020-01-01 10:00:00'), (1002, '牛客2号', 1200, 3, '算法', '2020-02-01 10:00:00'), (1003, '牛客3号♂', 22, 0, '算法', '2020-01-02 10:00:00'), (1004, '牛客4号', 25, 0, '算法', '2020-01-02 11:00:00'), (1005, '牛客555号', 4000, 7, 'C++', '2020-01-11 10:00:00'), (1006, '666666', 3000, 6, 'C++', '2020-11-01 10:00:00');
输出:
1001|牛客1|2020-01-01 10:00:00 1003|牛客3号♂|2020-01-02 10:00:00 1004|牛客4号|2020-01-02 11:00:00
Mysql 解法, 执行用时: 36ms, 内存消耗: 6388KB, 提交时间: 2021-12-14
SELECT uid , nick_name,register_time from user_info order by register_time LIMIT 3;
Mysql 解法, 执行用时: 36ms, 内存消耗: 6516KB, 提交时间: 2021-12-07
select uid, nick_name, register_time from user_info order by register_time limit 3
Mysql 解法, 执行用时: 36ms, 内存消耗: 6580KB, 提交时间: 2022-01-25
select uid,nick_name,register_time from (select uid,nick_name,register_time, ROW_NUMBER() over(order by register_time) rank_time from user_info) a where rank_time<=3
Mysql 解法, 执行用时: 36ms, 内存消耗: 6608KB, 提交时间: 2022-01-01
select uid, nick_name, register_time from user_info order by register_time limit 3
Mysql 解法, 执行用时: 37ms, 内存消耗: 6288KB, 提交时间: 2022-01-28
select uid,nick_name,register_time from user_info order by register_time limit 3