SQL CARTESIAN JOIN

CARTESIAN JOIN 或 CROSS JOIN 返回来自两个或多个连接表的记录集的笛卡尔积。因此,它等同于内部连接,其中连接条件的值始终为 True 或语句中不存在连接条件。

语法

CARTESIAN JOIN或CROSS JOIN的基本语法如下

SELECT table1.column1, table2.column2...
FROM  table1, table2 [, table3 ]

示例

现在我们看如下的两张表

1. CUSTOMERS 表

+----+----------+-----+-----------+----------+
| ID | NAME     | AGE | ADDRESS   | SALARY   |
+----+----------+-----+-----------+----------+
|  1 | Ramesh   |  32 | Ahmedabad |  2000.00 |
|  2 | Khilan   |  25 | Delhi     |  1500.00 |
|  3 | kaushik  |  23 | Kota      |  2000.00 |
|  4 | Chaitali |  25 | Mumbai    |  6500.00 |
|  5 | Hardik   |  27 | Bhopal    |  8500.00 |
|  6 | Komal    |  22 | MP        |  4500.00 |
|  7 | Muffy    |  24 | Indore    | 10000.00 |
+----+----------+-----+-----------+----------+

2. ORDERS 表

+-----+---------------------+-------------+--------+
| OID | DATE                | CUSTOMER_ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 100 | 2009-10-08 00:00:00 |           3 |   1500 |
| 101 | 2009-11-20 00:00:00 |           3 |   1560 |
| 102 | 2009-10-08 00:00:00 |           3 |   3000 |
| 103 | 2008-05-20 00:00:00 |           4 |   2060 |
| 104 | 2008-05-21 00:00:00 |           9 |   2160 |
+-----+---------------------+-------------+--------+

现在,让我们使用 CARTESIAN JOIN 连接这两个表,如下所示 -

SQL> SELECT  ID, NAME, AMOUNT, DATE
   FROM CUSTOMERS, ORDERS;

结果如下:

+----+----------+--------+---------------------+
| ID | NAME     | AMOUNT | DATE                |
+----+----------+--------+---------------------+
|  1 | Ramesh   |   1500 | 2009-10-08 00:00:00 |
|  1 | Ramesh   |   1560 | 2009-11-20 00:00:00 |
|  1 | Ramesh   |   3000 | 2009-10-08 00:00:00 |
|  1 | Ramesh   |   2060 | 2008-05-20 00:00:00 |
|  1 | Ramesh   |   2160 | 2008-05-21 00:00:00 |
|  2 | Khilan   |   1500 | 2009-10-08 00:00:00 |
|  2 | Khilan   |   1560 | 2009-11-20 00:00:00 |
|  2 | Khilan   |   3000 | 2009-10-08 00:00:00 |
|  2 | Khilan   |   2060 | 2008-05-20 00:00:00 |
|  2 | Khilan   |   2160 | 2008-05-21 00:00:00 |
|  3 | kaushik  |   1500 | 2009-10-08 00:00:00 |
|  3 | kaushik  |   1560 | 2009-11-20 00:00:00 |
|  3 | kaushik  |   3000 | 2009-10-08 00:00:00 |
|  3 | kaushik  |   2060 | 2008-05-20 00:00:00 |
|  3 | kaushik  |   2160 | 2008-05-21 00:00:00 |
|  4 | Chaitali |   1500 | 2009-10-08 00:00:00 |
|  4 | Chaitali |   1560 | 2009-11-20 00:00:00 |
|  4 | Chaitali |   3000 | 2009-10-08 00:00:00 |
|  4 | Chaitali |   2060 | 2008-05-20 00:00:00 |
|  4 | Chaitali |   2160 | 2008-05-21 00:00:00 |
|  5 | Hardik   |   1500 | 2009-10-08 00:00:00 |
|  5 | Hardik   |   1560 | 2009-11-20 00:00:00 |
|  5 | Hardik   |   3000 | 2009-10-08 00:00:00 |
|  5 | Hardik   |   2060 | 2008-05-20 00:00:00 |
|  5 | Hardik   |   2160 | 2008-05-21 00:00:00 |
|  6 | Komal    |   1500 | 2009-10-08 00:00:00 |
|  6 | Komal    |   1560 | 2009-11-20 00:00:00 |
|  6 | Komal    |   3000 | 2009-10-08 00:00:00 |
|  6 | Komal    |   2060 | 2008-05-20 00:00:00 |
|  6 | Komal    |   2160 | 2008-05-21 00:00:00 |
|  7 | Muffy    |   1500 | 2009-10-08 00:00:00 |
|  7 | Muffy    |   1560 | 2009-11-20 00:00:00 |
|  7 | Muffy    |   3000 | 2009-10-08 00:00:00 |
|  7 | Muffy    |   2060 | 2008-05-20 00:00:00 |
|  7 | Muffy    |   2160 | 2008-05-21 00:00:00 |
+----+----------+--------+---------------------+

查看笔记

扫码一下
查看教程更方便