SQL288. 今天的刷题量(一)
描述
id | subject_id | create_time |
1 | 2 | 2999-02-11 |
2 | 2 | 2999-02-21 |
3 | 1 | 2999-02-21 |
4 | 1 | 2999-02-22 |
5 | 3 | 2999-02-22 |
6 | 2 | 2999-02-22 |
7 | 2 | 2999-02-22 |
id | name |
1 | jzoffer |
2 | tiba |
3 | huaweijishu |
4 | top101 |
name | cnt |
tiba | 2 |
jzoffer | 1 |
huaweijishu | 1 |
示例1
输入:
drop table if exists submission; CREATE TABLE submission ( id int(11) NOT NULL, subject_id int(11) NOT NULL, create_time date NOT NULL ); CREATE TABLE subject ( id int(11) NOT NULL, name varchar(32) NOT NULL ); INSERT INTO submission VALUES (1,2,'2999-02-11'), (2,2,'2999-02-21'), (3,1,'2999-02-21'), (4,1,'2999-02-22'), (5,3,'2999-02-22'), (6,2,'2999-02-22'), (7,2,'2999-02-22'); INSERT INTO subject VALUES (1,'jzoffer'), (2,'tiba'), (3,'huaweijishi'), (4,'top101');
输出:
name|cnt tiba|2 jzoffer|1 huaweijishi|1
Sqlite 解法, 执行用时: 11ms, 内存消耗: 3524KB, 提交时间: 2022-04-17
select name,count(name) cnt from submission sn join subject st on subject_id=st.id where create_time=current_date group by name order by cnt desc,subject_id
Sqlite 解法, 执行用时: 12ms, 内存消耗: 3492KB, 提交时间: 2022-04-17
select name,count(st.name) cnt from subject st join submission sn on st.id=sn.subject_id where create_time =current_date group by st.name order by cnt desc ,sn.subject_id
Sqlite 解法, 执行用时: 12ms, 内存消耗: 3496KB, 提交时间: 2022-04-02
select s2.name,count(*) cnt from submission s1 join subject s2 on s1.subject_id=s2.id where create_time=current_date group by s2.name order by cnt desc,s1.subject_id asc;
Sqlite 解法, 执行用时: 12ms, 内存消耗: 3516KB, 提交时间: 2022-05-01
select s2.name,count(*) cnt from (select subject_id from submission where create_time = CURRENT_DATE ) s1 left join subject s2 on s1.subject_id = s2.id group by s2.name order by count(*) desc,s1.subject_id;
Sqlite 解法, 执行用时: 12ms, 内存消耗: 3520KB, 提交时间: 2022-05-08
select tb2.name, count(*) as cnt from submission as tb1 left join subject as tb2 on tb1.subject_id=tb2.id where tb1.create_time=current_date group by tb2.name, tb1.subject_id order by cnt desc, tb1.subject_id
上一题