Database project--Cartesian product

Source: Internet
Author: User
SQL Cartesian productIn mathematics, two sets of X and Y Cartesian product(Cartesian product), also known as Direct Product, represented as XXY, is all possible ordered pairs whose first object is a member of X and the second object is a member of Y. suppose set a={a,b}, set b={0,1,2}, then the Cartesian product of two sets is {(a,0), (a,1), (a,2), (b,0), (b,1), (b,2)}.
/*=======================================
Description:
Implementing Cartesian product with SQL
Author: CC
Date: 2011.11.03
=======================================*/

Use Test
Go

SELECT * from L; --The following figure L
SELECT * from M; --The following figure m

--Cross connection implementation, LM
SELECT * from L CROSS JOIN m;

L table

Lid name
----------- --------------------
101 Beijing
102 Taiyuan
NULL
102 NULL

(4 rows affected)

M table
Matid Qty Lid mname
----------- ----------- ----------- --------------------
10011 1 101 Beijing
20012 1 102 Taiyuan
10011 1 102 Taiyuan
10011 1 102 Taiyuan

Lm:

Lid name Matid Qty Lid mname
----------- -------------------- ----------- ----------- ----------- --------------------
101 Beijing 10011 1 101 Beijing
102 Taiyuan 10011 1 101 Beijing
NULL 10011 1 101 Beijing
102 NULL 10011 1 101 Beijing
101 Beijing 20012 1 102 Taiyuan
102 Taiyuan 20012 1 102 Taiyuan
NULL 20012 1 102 Taiyuan
102 NULL 20012 1 102 Taiyuan
101 Beijing 10011 1 102 Taiyuan
102 Taiyuan 10011 1 102 Taiyuan
NULL 10011 1 102 Taiyuan
102 NULL 10011 1 102 Taiyuan
101 Beijing 10011 1 102 Taiyuan
102 Taiyuan 10011 1 102 Taiyuan
NULL 10011 1 102 Taiyuan
102 NULL 10011 1 102 Taiyuan

(16 rows affected)

How does the Cartesian product of a relationship with more than 1 properties count? For example:
relationship r:
A1 A2 A3
a  b  c
b  a  c
c  a  b
relationship s:
A1 A2 A3
b  a  c
a  b  c  a  b
excuse me, what is the relationship Rx s result? Specific how to calculate it.
3 columns and 1 columns.

Calculate by Line ~ ~ You can think of 3 columns per row as a whole (as 1 columns)
A1 A2 A3 A1 A2 A3 A c a b c a b c a B c c a c a C b C b c A b b a c b
b A B c c a B
b a c c a b c a B

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.