mysql query with condition, join table query

---Restore content begins---

1, used to set whether the selected data allows duplicate rows (exactly the same data row)

all : Allowed - the default is All (allowed) if not written.

distinct : not allowed - this is the so-called "eliminate duplicate rows"

2, where: condition

3, group by: The grouping is based on the field name of the table followed by the table, usually only one field is grouped

MySQL table query syntax form: select [all | distinct] field name or expression from table name [where] [group by] [having] [order by] [limit];

Practice questions: There are four tables in total: Student table: student Teacher table: teacher Course table: course Grade table: score

          

1. Query the average score of courses starting with 3 that at least 5 students have taken in the Score table

--Operation table score, grouped by cno and cno starts with 3, take out the total number of each cno, and the average value of the degree corresponding to cno

select count(cno),avg(degree) from score where cno like '3%' group by cno;

 

--Find the total number of cno greater than 5 from the virtual table above

select * from

(select count(cno) a,avg(degree) b from score where cno like '3%' group by cno) c

where a>5;

2, query the Sno, Cname and Degree columns of all students

--Find the student's sno
 select sno from student;


 -找到 score 的 sno, degree, cno
 select sno, degree, cno from score;


 --find the course's cno
 select cno, cname from course;


 --组成新表cno sno degree
 select a.cno,b.sno,b.sname,a.degree from (select sno,degree,cno from score) a  join (select sno,sname from student) b on a.sno=b.sno;


 --组成有cname cno sn degree sname的表
 select d.cname,e.cno,e.sno,e.degree,e.sname
 from
   (select a.cno,b.sno,b.sname,a.degree from (select sno,degree,cno from score) a  join (select sno,sname from student) b on a.sno=b.sno) as e
  join
  (select cname,cno from course) as d
  on d.cno=e.cno;

3. Query the average score of students in class "95033"

-- Take the sno class from the student, the condition is that the class is 95033

select sno,class from student where class='95033';

--Remove the sno degree in the score

select sno,degree from score;

-- Combine the above two tables into one, and take the average of degrees

select avg(degree) from

(select sno,class from student where class='95033') a

join

(select sno,degree from score) b

on a.sno=b.sno;

4. Query the records of all students whose grades in the elective "3-105" courses are higher than those of the "109" students

--Take out the score of No. 109

select degree from score where sno='109' and cno='3-105';

-- get the final table

select * from score

where

degree>(select degree from score where sno='109' and cno='3-105')

and cno='3-105';

5. Query the Sno, Sname and Sbirthday columns of all students born in the same year as the student whose student number is 108

--Find Sbirthday with sno 108 in student
select year(sbirthday) from student where sno='108';


-- Get the final table
select sno,sname,sbirthday from student

where

year(sbirthday)=(select year(sbirthday) from student where sno='108');

 

---End of recovery content---

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324813700&siteId=291194637