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