Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A simple example of a query with multiple tables in mysql

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

The following is a simple example of a query with multiple tables in mysql. The secret of the text is that it is relevant to the topic. So, no gossip, let's go straight to the following, I believe that after reading the simple example of mysql multi-table query this article will certainly benefit you.

Import hellodb.sql to generate the database and enter it into the database

Mysql-uroot

< hellodb.sqluse mysql (1)以ClassID分组,显示每班的同学的人数 `select classid,count(stuid) from students group by classid``

(2) grouped by Gender to show the sum of their ages.

Select gender,sum (age) from students group by gender

(3) grouped by ClassID to show the classes whose average age is more than 25.

Select classid,avg (age) as new_age from students group by classid having new_age > 25

(4) grouping by Gender to show the sum of the ages of the students who are over 25 in each group.

Select gender,sum (age) from students group by gender

(5) display the names, courses and grades of the first five students

Select name,course,score from (select name,score,courseid from (select * from students where stuid80) as t inner join students on students.stuid=t.stuid) as t inner join courses on courses.courseid=t.courseid

(7) calculate the average scores of each of the top 8 students in their own two courses, and arrange them in descending order.

Select t.Stuidjig (score) from (select stuid,courseid from (select from students where stuid (select avg (age) from students))

(11) the names of students who indicate that the courses they are studying are courses 1, 2, 4 or 7.

Select name,courseid from (select * from coc where CourseIDin) as new inner join students on students.classid=new.classid

(12) students who are older than the average age of their classmates in classes with at least 3 members

Select * from (select name,classid,age from students) ass inner join (select new.classid,avg (age) as cc from (select classid from students group by ClassID having count (stuid) > = 3) as new inner join students on students.classid=new.classid group by new.classid) as an on a.classid=s.classid where cc

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 280

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report