Data demonstration of Cartesian product, inner join and outer joins in "SQL" SQL

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.