Multiple table query

1. According to the three tables

Second, the statement

-Query the information of the youngest student (first sort all information according to age, and then take the first value is the smallest, flexible application)
-select * from students order by age limit 1

-Cartesian product = multiply the number of two tables

-- SELECT * FROM courses,scores WHERE courses.courseNo=scores.courseNo
-- SELECT * FROM courses as a,scores as c WHERE a.courseNo=c.courseNo


-Query student information and student's score (two tables connected) -SELECT * FROM students, scores WHERE students.studentNo = scores.studentNo
-SELECT a.name, c. Score from students a, scores c WHERE a. studentNo = c.studentNo

-Query student information and student scores Connection within two tables  
-Syntax: select * FROM table 1 INNER JOIN table 2 on According to the conditions of the two table connection
-SELECT stu.name, sco.score FROM students stu INNER JOIN scores sco on stu.studentNo = sco.studentNo


-Query student information and the corresponding score of the student's course (the three tables are connected ,,, name ... is an alias) -SELECT stu.name name, sco.score score, cou.name course name FROM students stu, scores sco, courses cou 
-WHERE stu.studentNo = sco.studentNo and sco.courseNo = cou.courseNo

- Query student information and student courses corresponding results (within three tables linked ,,, name ... is an alias)
- Syntax: select * FROM table INNER JOIN Table II on a condition based on two tables connected
- -SELECT stu.name name, sco.score score, cou.name course number FROM students stu
-INNER JOIN scores sco on stu.studentNo = sco.studentNo
-INNER JOIN courses cou on sco.courseNo = cou.courseNo


-To query for small grades, it is required to display the name, course number, and grade (connected in the three tables ,, name ... is an alias) -select stu.name, sco.score, cou.`name` from students stu 
-inner join scores sco on stu.studentNo = sco.studentNo
-inner join courses cou on cou.courseNo = sco.courseNo
-where stu.`name` = '小小'


-Query small database scores, request to display name, course name, grades -select stu.name, sco.score, cou.`name` from students stu 
-inner join scores sco on stu.studentNo = sco.studentNo
-inner join courses cou on cou.courseNo = sco.courseNo
-where stu.`name` = 'little' and cou.`name` = 'database'


-Method one: Query the highest score among male students, requiring the display of name, course name, score -select stu.name, stu.sex, sco.score, cou.`name` from students stu 
-inner join scores sco on stu .studentNo = sco.studentNo
-inner join courses cou on cou.courseNo = sco.courseNo
-where sex = '
Male'-ORDER BY sco.score DESC
-LIMIT 1

- Method Two: boys query highest score required to display the name, course name, score
- the SELECT stu.name, stu.sex, sco.score, cou.`name` from STU Students, Scores SCO, courses COU
- stu.studentNo = sco.studentNo WHERE
- and sco.courseNo = cou.courseNo
- and Sex = 'M'
- the ORDER BY sco.score DESC
- limit. 1

- left connecting syntax
- select * from Table 1 
-left join Table 2 on Table 1. Same condition = Table 2. Same condition


-Query the scores of all students, including students without grades -SELECT stu.name, sco.score FROM students stu 
-LEFT JOIN scores sco on stu.studentNo = sco.studentNo


-Query the scores of all students, including those without grades, need to display the course name -SELECT stu.name, sco.score, cou.`name` FROM students stu 
-LEFT JOIN scores sco on stu.studentNo = sco. studentNo
-LEFT JOIN courses cou on sco.courseNo = cou.courseNo

-Right join Syntax 
-select * from Table 1
-right join Table 2 on Table 1. Same condition = Table 2. Same condition

-Query the
scores of all courses, including courses without grades -SELECT * FROM scores sco 
-RIGHT JOIN courses cou on sco.courseNo = cou.courseNo


-Query the scores of all courses, including courses without grades, including students -SELECT sco.score, cou.`name`, stu.`name` FROM scores sco 
-RIGHT JOIN courses cou on sco.courseNo = cou. courseNo
-RIGHT JOIN students stu on stu.studentNo = sco.studentNo


 

Published 29 original articles · received 1 · views 595

Guess you like

Origin blog.csdn.net/wennie11/article/details/104675661