preface

In this chapter, we will learn the advanced parts of SQL queries, such as inner joins, outer joins, and subqueries, with which we will be able to solve complex query problems in our projects.


Foreign key constraints

MySQL belongs to a relational database, the relationship between tables can be established, such as: student table and score table, add student number in the score table reference student number, so in the score table do not need to add duplicate student information, this relationship is also called primary foreign key relationship, can be set by setting foreign key constraint implementation. Add a foreign key constraint to a table to ensure referential integrity between tables. Add a foreign key constraint to a table to ensure referential integrity between tables.

Create table table name (table name type constraint,... , constraint foreign key name Foreign key references primary tableCopy the code

Code examples:

use mydb; Drop table if exists tb_score; create table tb_score ( score_id int primary key auto_increment, score_stu_id int, score int, score_course varchar(20), constraint fk_score_stu_id foreign key(score_stu_id) references tb_student(stu_id) );Copy the code

Inner join query

In the query, we often want to put the related fields of multiple tables, together with the query, such as the query of student results, to show the score and student name. At this time we need to join query, join query is divided into inner join and outer join, we first learn inner join query. Inner join query features: it will query data existing in related tables. Syntax can be implemented in two ways:

1) select..... Select * from inner join table 2 on inner join table 1 Foreign keys;Copy the code

Note: Table 1 is assumed to be the primary table and the order of the inner join table is irrelevant

2) select..... Select * from table 1 where table 1 = 1 where table 2 = 1 Foreign keys;Copy the code

Code examples:

1 select s.tu_id, s.tu_name, c.core_course, c.core from tb_score c inner join tb_student s on s.tu_id = c.score_stu_id; Select s.stu_id,s.stu_name, c.core_course, c.core from tb_score c, tb_student s where s.stu_id = c.score_stu_id;Copy the code

The effect is the same:

External join query

The outer connection is divided into left outer connection and right outer connection:

  1. The left outer join is used to query the data of multiple tables and display all the data in the left table. If the data in the right table does not match the data in the left table, null is added. Grammar:
The select field... From left table left join right table on main table. Primary key = child table. Foreign keys;Copy the code

Code examples:

-- Left outer connection, Select s.tu_id, s.tu_name, c.core_course, c.core from tb_student s left join tb_score c on s.tu_id = select * from tb_student s left join tb_score c on s.tu_id = c.score_stu_id;Copy the code



Query all students who have taken exams

select s.stu_id,s.stu_name,c.score_course,c.score from tb_student s
 left join tb_score c on s.stu_id = c.score_stu_id where c.score is not null; 
Copy the code

Select s.tu_id, s.tu_name, c.core_course, c.core from tb_student s left join tb_score c on s.tu_id = select * from tb_student s left join tb_score c on s.tu_id = c.score_stu_id where c.score is null;Copy the code



2) Right outer connection

In contrast to the left join, all data from the right table is displayed, and inconsistencies in the left table are nulled.

Grammar:

The select field... From left table right join right table on main table Primary key = child table. Foreign keys;Copy the code

Code examples:

select s.stu_id,s.stu_name,c.score_course,c.score from tb_score c right join tb_student s 
on s.stu_id = c.score_stu_id;
Copy the code

The subquery

In the query statement can also be embedded query statement, embedded query is also called sub-query, sub-query will be executed first, after the query result, the parent query can use the result as a query condition for another query, which can solve the more complex query problem. Grammar:

select ... Select * from table where (select... From table where condition);Copy the code

Code examples:

Select * from tb_student where stu_age > (select stu_age from tb_student where stu_name = 'tb_student ');Copy the code

Select * from tb_student where stu_address = (select stu_address from tb_student where stu_name = 'tb_student ');Copy the code

IN of the subquery

Sometimes the use of comparison operators can cause errors when there is more than one query result in a subquery, so we need to use some keywords to help filter the results. The in keyword is used to equal either of the fields or data in the list. Code examples:

Select * from student where name and grade have been used; select * from student where name and grade have been used Select stu_name,score_course,score from tb_student inner join tb_score on tb_student.stu_id = select stu_name,score_course,score from tb_student inner join tb_score on tb_student.stu_id = Tb_score. Score_stu_id WHERE score =' score 'and score in(select score from tb_score where score =' score ');Copy the code

ALL of the subquery

All is used with the comparison operator, and the result is valid only if the field and all query results are compared. Grammar:

Field comparison operation all(query result)Copy the code

Code examples:

If the female student is younger than all the male students, start with the age of all the male students. If the female student is older than all these ages, Select stu_name,stu_age,stu_gender from tb_student where stu_gender = 'female' and stu_age < all(select stu_age from tb_student Tb_student where stu_gender = 'male ');Copy the code

ANY of the subqueries

The any operator is used with the comparison operator. If the field is compared to any of the query results, the result is valid. Grammar:

Field comparison operation any(query result)Copy the code

Code examples:

Select stu_name,stu_address from tb_student where stu_address = 'wuhan' and stu_age > any(select * from tb_student where stu_address = 'wuhan' and stu_age > any Stu_age from tb_student where stu_address=' 98 ');Copy the code

Exists of the subquery

Exists indicates whether a query result exists. If there is no result, false is returned; if there is a result, true is returned.

Exists (Query result)

Select * from student where English test is passed, Select * from tb_student where exists(select score_stu_id from tb_score where score_stu_id exists) select * from tb_score where score_stu_id exists Tb_student. stu_id = tb_score_stu_id and score_course = 'English ');Copy the code

conclusion

In this chapter we learned the inner join, outer join, sub-query and other advanced query methods, sometimes the comprehensive use of these query methods is a certain difficulty, when we are familiar with them, query data is not difficult. Click “like” if it works 🙂