Multi-table query (JOIN)

data preparation

We need two tables  student and student_score 

CREATE TABLE `student` (
  `student_id` int NOT NULL,
  `name` varchar(45) NOT NULL,
  PRIMARY KEY (`student_id`)
);

CREATE TABLE `student_score` (
  `student_id` int NOT NULL,
  `subject` varchar(45) NOT NULL,
  `score` int NOT NULL
);

then insert the data

INSERT INTO `student` (`student_id`, `name`)
VALUES (1,'Tim'),(2,'Jim'),(3,'Lucy');

INSERT INTO `student_score` (`student_id`, `subject`, `score`)
VALUES (1,'English',90),(1,'Math',80),(2,'English',85),
       (2,'Math',88),(5,'English',92);

Table Structure

 Cross connection (CROSS JOIN)

A cross join returns the Cartesian product of two sets. That is, all possible combinations of all rows in the two tables. This is equivalent to the inner connection without a connection condition or the connection condition is always true.

If a  m table with rows and another  n table with rows, their cross join will return row  m * n data.

If there are three data of abc and cross connection of three data of efg, a is connected to efg respectively, b is connected to efg respectively, and c is connected to efg respectively;

Example:

SELECT student.*,student_score.* FROM student CROSS JOIN student_score;

Usually, columns can be added after SELECT, but now we have multi-table joint query, and we always have to distinguish which table’s column is not, so it is the table name. The column name indicates the column

At the same time, the abbreviation is

SELECT student.*,student_score.* FROM student CROSS JOIN student_score;

operation result:

Inner join (INNER JOIN)

An inner join compares each row of the first table with each row of the second table and combines the rows of both tables as one row in the result set if the given join condition is met.

SELECT 
student.*,student_score.* 
FROM 
student INNER JOIN student_score 
ON 
student.student_id = student_score.student_id;

Inner joins combine data from two tables based on the join condition. Inner joins are equivalent to cross joins with filter conditions added.

So this line of code is also equivalent to

SELECT 
student.*,student_score.* 
FROM 
student,student_score 
WHERE 
student.student_id = student_score.student_id;

If we only need the same column name for equivalent matching, we can also use USING

SELECT
  student.*,
  student_score.*
FROM
  student
  INNER JOIN student_score USING(student_id);

Outer joins are divided into left outer joins and right outer joins 

left join

Left join is the abbreviation of left outer join, which requires a join condition.

When two tables are left joined, the first table is called the left table and the second table is called the right table. For example  A LEFT JOIN B, A is the left table, B is the right table.

The left join is based on the data rows of the left table , and matches each row of the right table according to the connection. If the match is successful, the rows of the left table and the right table will be combined into a new data row and returned; if the match is unsuccessful, the rows of the left table and NULL values ​​are combined into a new data row and returned.

SELECT
  student.*,
  student_score.*
FROM
  student
  LEFT JOIN student_score
  ON student.student_id = student_score.student_id;

First of all, these rows are not in one-to-one correspondence. With the student table as the main body, each row of the student table can match multiple rows of student_score data. Consider it with natural thinking! There are multiple student_ids in the score table and student_id in the student Equal, do you think we can have one or not? That's for sure.

 We can see that the student_id in student_score has a 5. This 5 has no corresponding in the student table, so it is filled with NULL

This ON can still be replaced by USING (when the query column name is the same), so this replacement is for ON rather than a specific connection

right join

The logic is the same as the left join, but this time the parameter on the right is the main body

Even say that A LEFT JOIN B is equivalent to B RIGHT JOIN A

Guess you like

Origin blog.csdn.net/chara9885/article/details/131483795
Recommended