CARTESIAN JOIN或CROSS JOIN从两个或多个联接表中返回记录集的笛卡尔积。
CARTESIAN JOIN - 语法
CARTESIAN JOIN 或 CROSS JOIN 的基本语法如下-
SELECT table1.column1, table2.column2... FROM table1, table2 [, table3 ]
CARTESIAN JOIN - 示例
请考虑以下两个表。
表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 | Learnfk | 24 | Indore | 10000.00 | +----+----------+-----+-----------+----------+
表2:ORDERS表如下-
+-----+---------------------+-------------+--------+ |OID | DATE | CUSTOMER_ID | AMOUNT | +-----+---------------------+-------------+--------+ | 102 | 2019-10-08 00:00:00 | 3 | 3000 | | 100 | 2019-10-08 00:00:00 | 3 | 1500 | | 101 | 2019-11-20 00:00:00 | 2 | 1560 | | 103 | 2018-05-20 00:00:00 | 4 | 2060 | +-----+---------------------+-------------+--------+
现在,让无涯教程使用CARTESIAN JOIN联接这两个表,如下所示:
SQL> SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS, ORDERS;
这将产生以下输出-
+----+----------+--------+---------------------+ | ID | NAME | AMOUNT | DATE | +----+----------+--------+---------------------+ | 1 | Ramesh | 3000 | 2019-10-08 00:00:00 | | 1 | Ramesh | 1500 | 2019-10-08 00:00:00 | | 1 | Ramesh | 1560 | 2019-11-20 00:00:00 | | 1 | Ramesh | 2060 | 2018-05-20 00:00:00 | | 2 | Khilan | 3000 | 2019-10-08 00:00:00 | | 2 | Khilan | 1500 | 2019-10-08 00:00:00 | | 2 | Khilan | 1560 | 2019-11-20 00:00:00 | | 2 | Khilan | 2060 | 2018-05-20 00:00:00 | | 3 | kaushik | 3000 | 2019-10-08 00:00:00 | | 3 | kaushik | 1500 | 2019-10-08 00:00:00 | | 3 | kaushik | 1560 | 2019-11-20 00:00:00 | | 3 | kaushik | 2060 | 2018-05-20 00:00:00 | | 4 | Chaitali | 3000 | 2019-10-08 00:00:00 | | 4 | Chaitali | 1500 | 2019-10-08 00:00:00 | | 4 | Chaitali | 1560 | 2019-11-20 00:00:00 | | 4 | Chaitali | 2060 | 2018-05-20 00:00:00 | | 5 | Hardik | 3000 | 2019-10-08 00:00:00 | | 5 | Hardik | 1500 | 2019-10-08 00:00:00 | | 5 | Hardik | 1560 | 2019-11-20 00:00:00 | | 5 | Hardik | 2060 | 2018-05-20 00:00:00 | | 6 | Komal | 3000 | 2019-10-08 00:00:00 | | 6 | Komal | 1500 | 2019-10-08 00:00:00 | | 6 | Komal | 1560 | 2019-11-20 00:00:00 | | 6 | Komal | 2060 | 2018-05-20 00:00:00 | | 7 | Learnfk | 3000 | 2019-10-08 00:00:00 | | 7 | Learnfk | 1500 | 2019-10-08 00:00:00 | | 7 | Learnfk | 1560 | 2019-11-20 00:00:00 | | 7 | Learnfk | 2060 | 2018-05-20 00:00:00 | +----+----------+--------+---------------------+
参考链接
https://www.learnfk.com/sql/sql-cartesian-joins.html
标签:10,00,CARTESIAN,JOIN,08,无涯,2019,2018,20 From: https://blog.51cto.com/u_14033984/9287113