A collection of database relational operations

traditional set operations


Traditional set operations include union (UNION), difference set (EXCEPT or MINUS or LEFT JOIN&&IS NULL), intersection (INTERSECT or INNER JOIN), and Cartesian product (JOIN) .

It should be noted that the syntax of different databases may be somewhat different, but the general concept is what this paragraph talks about.

1: Union (UNION)

Combined effect: delete duplicate rows.

It will compare whether the contents of all the same columns of the two tables are consistent, but you should not use the data of n columns of one table and the data of another n+1 columns to use UNION, otherwise an error will occur.

For example, the data of people1 in Table 1 is as follows:

Please add a picture description

For example, the data of people2 in Table 2 is as follows:

Please add a picture description

Use UNION:

select * from people1 
union 
select * from people2;

insert image description here

2: Difference (EXCEPT or MINUS or LEFT JOIN&&IS NULL)

Subtraction: Find elements that exist in one set but not in the other.

  1. The number of comparison columns must be the same, and it is not possible to compare n columns of one set with n+1 columns of another set. (Through the content of the above paragraph, you may be a little confused. In fact, when select * is not used, there will be a comparison between select name from people1 and select name, source from people2)
  2. The data types of the corresponding columns need to be compatible. The column of one set is of type int and the column of another set of varchar should not perform set operation.

MySQL does not support EXCEPT or MINUS, you can use LEFT JOIN&&IS NULL to achieve, this is not necessary, other methods that can achieve the same logic are also possible.

PostgreSQL and Oracle support EXCEPT or MINUS.

Example: The data of people1 in Table 1 is as follows:

Please add a picture description

For example, the data of people2 in Table 2 is as follows:

Please add a picture description

Use difference (MySQL database):

select * from people1 
left join 
people2 
on people1.name = people2.name 
where people2.name is null;

join needs to be used together with on.

Using subtraction (PostgreSQL and Oracle databases):

select * from people1 
MINUS
select * from people2 

insert image description here

It can be seen that the row of data "Yasuo" does not exist in another collection.

3: Intersection (INTERSECT or INNER JOIN)

Intersection: Returns rows where both sets are equal.

MySQL does not support INTERSECT, you can use INNER JOIN to achieve the intersection function.

PostgreSQL and Oracle support INTERSECT.

Example: The data of people1 in Table 1 is as follows:

Example: The data of people1 in Table 1 is as follows:

Please add a picture description

For example, the data of people2 in Table 2 is as follows:

Please add a picture description

Using intersection (MySQL database):

select * from people1 
inner join 
people2 
on people1.name = people2.name;

insert image description here

It can be found that the row of data "Riven" exists in both sets.

4: Cartesian product (JOIN)

Cartesian products usually have join conditions:

  1. INNER JOIN (inner join): returns matching rows in both collections.
  2. LEFT JOIN (left connection): Return all the rows of the left table, and the matching rows of the right table, otherwise, the right table will return NULL.
  3. RIGHT JOIN (right connection): Return all the rows of the right table, and the matching rows of the left table, otherwise, the left table will return NULL.
  4. FULL JOIN (full connection): returns all the rows of the left table and the right table. When the row on one side does not match the row on the other side, NULL will be returned.

specialized set operations


Specialized collection operations include selection (SELECT), projection (PROJECTION), connection (JOIN), and division (DIVISION) .

1: Select (SELECT)

Selection function: select rows that meet certain conditions, usually implemented with WHERE

select * from people1 
where name = "亚索";

insert image description here

2: Projection (PROJECTION)

Projection function: select specific columns in the collection

select name,source 
from people1 
where name = "亚索";

insert image description here

3: Connection (JOIN)

Join role: Combine two tables, usually with INNER JOIN, LEFT JOIN, RIGHT JOIN or FULL JOIN.

4: Divide (DIVISION)

To use this well, we need to imagine an application scenario.

for example:

  1. We want to find people who bought all items
  2. We want to find the class that won all the awards
  3. We want to find employees who attended all events

For example, the data of people3 in Table 3 is as follows:

insert image description here

For example, the allproject data in Table 4 is as follows:

insert image description here

Use divide to find people who participated in all projects:

select name from people3 
group by name 
having count(distinct project) = (select count(*) from allproject);

insert image description here

The basic idea of ​​division (DIVISION): Use GROUP BY to group, and then use HAVING to judge whether the number of each group is equal to the number of the whole set.

Guess you like

Origin blog.csdn.net/qq_41974199/article/details/131024416