Introduction
A cross join is used to generate a Cartesian product of two or more tables (contains results for each row in all tables).
In mathematics, a Cartesian product is a mathematical operation that returns multiple sets of data.
For example: two sets: X {a, b, c}
and Y {1, 2, 3}
the Cartesian product is: (a, 1),(a, 2),(a, 3),(b, 1),(b, 2),(b, 3),(c, 1),(c, 2),(c, 3)
.
In SQL, the Cartesian product of A表
and B表
is the result set, with each row in the first paired with each row in 表A
the second . 表B
Assuming A表
there are m
rows and B表
there are n
rows, then the cross join result of A表
and has rows.B表
m * n
grammar
The first
select *
from A
cross join B;
the second
select *
from A, B;