Review classic examples of query statements 5

/*
Query the records of the three best scores in each subject
*/ #Method
1: Calculate the number of records that are greater than your own score, if it is less than 3, select, #Then
all selected results are sorted according to scores and course numbers
#group by After that, limit is not allowed

select * from score
 where (
    select count(*) from score as s
    where score.cid = s.cid and score.score < s.score
 ) < 3 order by cid asc, score.score desc;

#Method 2: Self-left handing, that is, first handing yourself with yourself

select * from score s left join score ss on s.cid = ss.cid and s.score < ss.score
order by s.cid,s.score;

#Query the list of students born in 1990

select * from student
where year(student.sage) = 1990;

#According to the date of birth, the current month and day <the date of birth, the age is reduced by one

select student.sid as 学生编号 , student.sname as 学生姓名,
TIMESTAMPDIFF(YEAR,student.sage,CURDATE()) as 学生年龄 from student;

#Query students whose birthdays last week

select * from student where weekofyear(student.sage) = weekofyear(CURDATE()) - 1;

#Query students who have a birthday this week

select * from student where weekofyear(student.sage) = weekofyear(CURDATE());

#Query students who will have their birthday next week

select * from student where weekofyear(student.sage) = weekofyear(CURDATE())+1;

#Query students whose birthdays last month

select * from student where month(student.sage) = month(curdate()) - 1;

#Query students who have a birthday this month

select * from student where month(student.sage) = month(curdate());

#Query students with birthdays next month

select * from student where month(student.sage) = month(curdate()) + 1;

 

Guess you like

Origin blog.csdn.net/calm_encode/article/details/113806082