头图

This question uses MySQL 8.0 and has not been tested in MySQL 5.6, so it is not guaranteed to be correct.

topic

Topic source: more than 5 students

5 out more than or equal to 061eb959b6dbd0 students' courses (student's courses are not double counted)

create table courses (
    student varchar(255),
    class varchar(255)
)

insert into courses values
('A', 'Math'),
('B', 'English'),
('C', 'Math'),
('D', 'Biology'),
('E', 'Math'),
('F', 'Computer'),
('G', 'Math'),
('H', 'Math'),
('I', 'Math');

SQL: Method 1

select class from courses group by class having count(*) >= 5;

Parse

The students' classes are not repeated, so they class grouped according to 061eb959b6dc36, and the students who are greater than or equal to 5 having

SQL: Method 2

select class from (
    select class, count(*) as num from courses group by class
) as c where num >= 5;

Parse

  • First find out the number of students in each course, use group by grouping
  • use this query as a temporary table
  • Query this table again, and the filter condition is that the number of people is greater than or equal to 5 .

More reference for solving problems: https://github.com/astak16/blog-mysql


uccs
759 声望91 粉丝

3年 gis 开发,wx:ttxbg210604