Mysql 简明教程
MySQL - Cross Join
MySQL Cross Join
将第一张表的每一行与第二张表的每一行组合在一起 MySQL Cross Join 。这是一种基本的内部连接类型,用于检索两个单独表的笛卡尔乘积(或交叉乘积)(即排列)。
A MySQL Cross Join combines each row of the first table with each row of second table. It is a basic type of inner join that is used to retrieve the Cartesian product (or cross product) of two individual tables (i.e. permutations).
以下示例图以简单的方式说明了交叉连接。
The sample figure below illustrates the cross join in a simple manner.

Syntax
下面是 MySQL 中交叉连接查询的基本语法 −
Following is the basic syntax of the Cross Join query in MySQL −
SELECT column_name(s)
FROM table1
CROSS JOIN table2
Example
在这个交叉连接的示例中,我们首先创建一个名为 CUSTOMERS 的表,其中包含客户的个人详细信息,包括姓名、年龄、地址和薪水等。
In this example of cross join, let us first create a table named CUSTOMERS, which contains personal details of customers including their name, age, address and salary etc.
CREATE TABLE CUSTOMERS (
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
现在使用 INSERT 语句向该表中插入值,如下所示:
Now insert values into this table using the INSERT statement as follows −
INSERT INTO CUSTOMERS VALUES
(1, 'Ramesh', 32, 'Ahmedabad', 2000.00 ),
(2, 'Khilan', 25, 'Delhi', 1500.00 );
该表将被创建为:
The table will be created as −
让我们创建一个另一个表 ORDERS ,其中包含已下的订单及其下的日期。
Let us create another table ORDERS, containing the details of orders made and the date they are made on.
CREATE TABLE ORDERS (
OID INT NOT NULL,
DATE VARCHAR (20) NOT NULL,
CUSTOMER_ID INT NOT NULL,
AMOUNT DECIMAL (18, 2),
);
使用 INSERT 语句像下面这样向该表中插入值:
Using the INSERT statement, insert values into this table as follows −
INSERT INTO ORDERS VALUES
(100, '2009-10-08 00:00:00', 3, 1500.00),
(101, '2009-11-20 00:00:00', 2, 1560.00);
该表显示如下:
The table is displayed as follows −
现在,如果我们对上面给出的这两个表执行以下 Cross Join query ,交叉连接将会将 CUSTOMERS 表中的每一行与 ORDERS 表中的每一行组合在一起。
Now, if we execute the following Cross Join query on these two tables given above, the cross join combines each row in CUSTOMERS table with each row in ORDERS table.
SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
CROSS JOIN ORDERS;
Joining Multiple Tables with Cross Join
我们也可以使用交叉连接连接多于两张表。在这种情况下,将显示多路排列,结果表预计将比单独的表包含更多的记录。
We can also join more than two tables using cross join. In this case, multiple-way permutations are displayed and the resultant table is expected to contain way more records than the individual tables.
Syntax
下面是使用 MySQL 中的交叉连接连接多张表的语法 −
Following is the syntax to join multiple tables using cross join in MySQL −
SELECT column_name(s)
FROM table1
CROSS JOIN table2
CROSS JOIN table3
CROSS JOIN table4
.
.
.
Example
现在组合三张表 CUSTOMERS、ORDERS 和 ORDER_RANGE,以使用交叉连接演示组合多张表。
Let us now combine three tables CUSTOMERS, ORDERS and ORDER_RANGE, to demonstrate combining multiple tables using cross join.
将使用以下查询创建 ORDER_RANGE 表 −
We will create the ORDER_RANGE table using the query below −
CREATE TABLE ORDER_RANGE (
SNO INT NOT NULL,
ORDER_RANGE VARCHAR (20) NOT NULL,
);
现在,我们可以使用 INSERT 语句将值插入到此空表中,如下所示:
Now, we can insert values into this empty tables using the INSERT statement as follows −
INSERT INTO ORDER_RANGE VALUES
(1, '1-100'),
(2, '100-200'),
(3, '200-300');
ORDER_RANGE 表如下 −
The ORDER_RANGE table is as follows −
现在对给定的表使用以下交叉连接查询,
Now we use the following cross join query on the given tables,
SELECT ID, NAME, AMOUNT, DATE, ORDER_RANGE
FROM CUSTOMERS
CROSS JOIN ORDERS
CROSS JOIN ORDER_RANGE;