Lecture 23: Concept of Cartesian Product of Multi-Table Query

1. The concept of Cartesian product

Descartes is a scientist who discovered the Cartesian product. In the multi-table query of the database, there is also the concept of the Cartesian product.

Cartesian product refers to all combinations of two sets, as shown in the following figure:

A and B are equivalent to two sets, and both need to be multiplied by 1, 2, 3, and 4. There are four combinations of A set and 1, 2, 3, and 4, and B set and 1, 2, 3, and 4 also have four types. In the case of combination, all the combined results of the two sets of A and B will eventually be output.

image-20220517222300670

The two sets of A and B are equivalent to 2 pieces of data in the table, 1, 2, 3, and 4 are equivalent to four independent pieces of data in another table. In the database, multi-table query is similar to the concept of Cartesian product. When directly querying multiple data tables at the same time, the number of displayed data bars is the product of the data bars of the two tables.

However, this display effect is not our ideal state. We hope that the result of the query is that the data in table A and table B are listed, and the rest is not required. The implementation is also very simple, just add the connection between the two tables. One condition, the redundant data can be eliminated.

As shown in the figure below, we only want the data of the combination of set A and 3, and the combination of set B and 2. At this time, only the previous connection condition needs to be added, and only the combined data that meets the condition can be displayed.

image-20220517222838278

2. Demonstrate the phenomenon of Cartesian products in databases and how to avoid them

First, query the data of the personnel information table and the department data table at the same time.

select * from ryxxb,bmxxb

The query results are as follows. The results are not our ideal data. At this time, the phenomenon of Cartesian product is applied. When the first surface and the second table after from are queried at the same time, all the combined condition results in the two tables are listed. It came out so that each person combined different departments, which now leads to a problem, we don't know which of these data is the correct data.

image-20220518214701108

The solution to this problem is also very simple. Add a connection condition. The bm_id field of the personnel information table and the id field of the department information table are related, so let's add a connection condition. When the bm_id of the personnel table is related to the id field of the department table If the id field data is consistent, it will be displayed, otherwise it will not be displayed.

select * from ryxxb,bmxxb where ryxxb.bm_id = bmxxb.id order by ryxxb.id asc;

The query effect is as follows. When the bm_id field of the personnel information table is equal to the id field of the department information table, it will be queried.

Guess you like

Origin blog.csdn.net/weixin_44953658/article/details/127087480