In SQL query statements, the Cartesian product operation is often used in the inner joins, outer joins, and the basis of connections.
In a simple SQL, maybe we can also distinguish how the data is connected, once the query is complicated, the brain also made a paste, and stumbled.
In this paper, we simply record the results of the connected data in the form of data, which can be browsed when confused.
Run as MySQL.
> The original table
Select * from T_user u;
Select * from T_address A;
Select * from T_phone p;
> Cartesian product
Select * from T_user, t_address; Select * from Inner join t_address;
> Internal Connection
--Example 3.1Select * fromt_user u, t_address awhereU.id=A.user_id;--Example 3.2Select * fromt_user uInner Joint_address awhereU.id=A.user_id;--Example 3.3Select * fromt_user uInner JoinT_address A onU.id=A.user_id;
--Example 3.4Select * fromt_user uInner Joint_address awhereU.id=A.user_id andA.address like '% lo Wu%';--Example 3.5Select * fromt_user uInner JoinT_address A onU.id=A.user_id andA.address like '% lo Wu%';
Example 3.4, example 3.5, although the result set is the same, according to its SQL semantics, the execution process should be different. (I do not know the principle, here is not to delve into it)
> Left Outer connection
Select * from left outerjoinson= A.user_id;
Multilayer outer Joins
Select * from leftouterjoinon= A.user_id left OuterJoinon= p.user_id;
select * t_user u left outer join t_address a on u.id Span style= "color: #808080;" >= a.user_id left outer join t_phone P on u.id = p.user_id left outer join t_phone p2 on u.id Span style= "color: #808080;" >= P2. user_id ;
> Right Outer connection
Select * from right outerjoinson= A.user_id;
Get such a query, the effect is better looking, obvious
Select * from t_address a Right outer Join on = A.user_id;
Data demonstration of Cartesian product, inner join and outer joins in "SQL" SQL