[Homework answer] MySQL database homework-'Query all records in the student table'-'Query the 2nd to 4 records of the student table'-'Query the information of students from the computer department and the English department from the student table'

Preface

       Today, the teacher finished the basics of MySQL database and sent a document, with some simple topic assignments for us to complete. Record your after-school exercises again to deepen your impression.


1. Title

Insert picture description here

Insert picture description here
Insert picture description here

2. Problem solving steps

1. Create a database and table. And add data to it.

Let's create the database data table in turn:
Insert picture description here

Then add data to it:
Insert picture description here

Insert picture description here

2. New query

Query all record statements in the student table:
select * from student;

Query the 2nd to 4th records of the student table:

select * from student LIMIT 1,3

search result:
Insert picture description here
Insert picture description here

Query the student's student number, name, and department in the student table.

select id,Name,department from student;

Query student information of computer department and English department from the student table

select * from student
where department = '计算机系' OR department ='英语系';

Query how many people there are in each department from the student table;

select department,COUNT(*)from student GROUP BY department;

Query the highest score of each subject from the score table

select c_name, MAX(grade) from score
GROUP BY c_name;

Query Li Si's test subjects (c_name) and test results (grade)


select * from score as s LEFT JOIN 
student as st ON s.stu_id = st.id where name = '李四';

Query all student information and exam information in a connected way

select * from score as s LEFT JOIN 
student as st ON s.stu_id = st.id;     

Calculate the total grade of each student

select name,SUM(grade) from score
LEFT JOIN student ON score.stu_id = student.id GROUP BY name; 

Calculate the average score for each test subject

select c_name,AVG(grade) from score GROUP BY c_name;   

Query information about students whose computer scores are lower than 95

select name from score LEFT JOIN student 
ON score.stu_id = student.id GROUP BY c_name HAVING AVG(grade
)<95;

Query information about students taking computer and English exams at the same time

select name from (select * from score where c_name = '计算机') 
as s inner JOIN (select * from score where c_name = '英语') 
as st on s.stu_id = st.stu_id
 INNER JOIN student on s.stu_id =student.id   

Sort computer test scores from high to low

select * from score where c_name='计算机' ORDER BY grade DESC; 

Query the student number from the student table and score table, and then merge the query results

select * from score as s INNER JOIN student as st ON s.stu_id = st.id;

Query the name, department, examination subjects and results of students surnamed Zhang or Wang

select * from score as s INNER JOIN
 student as st ON s.stu_id = st.id where name like '张%' OR
name like '王%'

All queries are the names, ages, faculties, examination subjects and results of students in Hunan

select * from score as s
INNER JOIN student as st ON s.stu_id= st.id where address like '湖南%'

to sum up

  1. LIMIT(n,m) is used for paging, the following parameter means to check m starting from n, and the subscript starting from 0.
  2. In means to include that, in('Computer Department','English Department'); There are only two departments, restriction conditions.
  3. To query how many people there are in each department, first write department first, write count(*) after, and finally group by department.
  4. The same is true for seeking the highest score. Put the subject name in the front, MAX(grade) in the back, and group by c_name.
  5. When two tables have data that needs to be queried at the same time, use left join to connect, fixed writing select * from xxx as x left join xxx on xx.id = xxx.id.
  6. As long as you query the information of two tables, you need to use table joins, one of which is named inner join with As and the other is also named with inner join on on.
  7. Sorting problem: write at the end, for example, order by XXx (table name) desc is descending order, dsc is ascending order.

Inspired by this blog, perfect and organize on this basis. If this article is helpful to you, please give me a thumbs up.

https://blog.csdn.net/x289231673/article/details/78365941

Guess you like

Origin blog.csdn.net/qq_43055855/article/details/109451241