MySQL database (7)

Table of contents

 1. Joint query

1.1 Inner join

1.2 Outer join

1.3 Self-connection

1.4 Subquery

1.5 Combined query


 1. Joint query

In actual development, data often comes from different tables, so joint queries of multiple tables are required. Multi-table query takes Cartesian product of data from multiple tables:

The following is an exercise on multi-table query, corresponding to table design and data insertion.

1.1 Inner join

grammar:

select field from table 1 alias 1 [inner] join table 2 alias 2 on connection condition and other conditions;
select field from table 1 alias 1, table 2 alias 2 where connection condition and other conditions

Case:

--Query the scores of "Xu Xian"
select student.name, score.score from student,score
where student.name = 'Xu Xian' and score.student_id = student.id;

or

select student.name, score.score from student join score
on student.name = '许仙' and student.id = score.student_id;

 --Query the total scores of all students and their personal information

select student.sn, student.name, student.email, sum(score.score)
from student, score
where student.id = score.student_id
group by score.student_id;

Query the scores of all students and their personal information:
select student.id, student.sn, student.name, score.score,course.name
from student,score,course
where student.id = score.student_id and score.course_id = course.id;

Here we find that the two names are not identifiable and can be used as aliases. We will not go into details here.

There are many application scenarios for inner connections. Specific issues need to be analyzed in detail. This article only shows some common scenarios. 

1.2 Outer join

Outer joins are divided into left outer joins and right outer joins. If a joint query is performed, if the table on the left is completely displayed, we say it is a left outer join ; if the table on the right is completely displayed, we say it is a right outer join.
grammar:

-- Left outer join, table 1 completely displays
the select field name from table name 1 left join table name 2 on connection condition;
-- Right outer join, table 2 completely displays
the select field name from table name 1 right join table name 2 on connection condition ;

Case:

Query the scores of all students and their personal information. If the student has no scores, it also needs to display
select * from student left join score on student.id = score.student_id;

It is found here that even if the "foreigners learn Chinese" students have no grades, the left table can be fully displayed using left join 

  select * from student right join score on student.id = score.student_id;

When querying using a right outer join, only the data in the right table can be completely displayed. If the data is empty, it cannot be queried.

1.3 Self-connection

Self-joining refers to querying by connecting itself in the same table (mostly used here when columns cannot be separated)

Case: Display all score information for "Computer Principles" with higher scores than "Java"

For the case, the score table is a general table with no corresponding columns, so self-join is used here.

First query the computer principles and Java IDs

select id, name from course where name = 'Java' or name = 'Computer Principles';

Then query the score information of computer theory that is higher than Java

select student.*, s1.score as Java, s2.score as 'Computer Principles'
from student, score as s1 , score as s2   ​​--You
must alias the table here, because there is only one table in the self-join, otherwise It will not be recognized!
where student.id = s1.student_id

and s1.student_id = s2.student_id

and s1.score < s2.score
and s1.course_id = 1

and s2.course_id = 3;

In the future process, complex sql statements should be simplified as much as possible, and the sql that can be split should be split as much as possible. The above is to split a complex sql into two, which will improve the efficiency of the query.

1.4 Subquery

Subquery refers to a select statement embedded in other SQL statements, also called a nested query (not recommended, just understand it)
Case: Query and classmates of "do not want to graduate"

select * from student where classes_id=(select classes_id from student where
name='不想毕业');

1.5 Combined query

In practical applications, in order to merge the execution results of multiple selects, you can use the set operators union and union all. When using UNION and UNION ALL, the fields in the result sets of the previous and subsequent queries need to be consistent.

  • The union operator is used to obtain the union of two result sets. When this operator is used, duplicate rows in the result set are automatically removed.

Query courses with ID less than 3 or with the name "English"

select * from course where id < 3
union
select * from course where name = '英文';

 

You can also use or instead

select * from course where id < 3 or name = '英文';

  •  union all This operator is used to obtain the union of two result sets. When using this operator, duplicate rows in the result set will not be removed.

Query the courses whose id is less than 3, or whose name is "Java"
select * from course where id<3
union all
select * from course where name='English';

 

Guess you like

Origin blog.csdn.net/x2656271356/article/details/131840247