SQLite 交叉连接 - CROSS JOIN

SQLite 中的交叉连接 ( CROSS JOIN ) 把第一个表的每一行与第二个表的每一行进行匹配

如果两个输入表分别有 3 列和 12 列,则结果表有 3 x 12 = 36

交叉连接 ( CROSS JOIN ) 有可能产生非常大的表,使用时必须谨慎,只在适当的时候使用它们

语法

SQLite 交叉连接 ( CROSS JOIN ) 的语法如下

SELECT ... FROM table_name1 CROSS JOIN table_name2 ...

例如下面的语句可以将 tbl_languagetbl_rank 两个表做一个交叉连接

SELECT tbl_language.id,tbl_language.name,tbl_rank.rank 
FROM tbl_language 
CROSS JOIN tbl_rank;

范例

先运行下面的 SQL 语句创建测试数据

DROP TABLE IF EXISTS `tbl_language`;
DROP TABLE IF EXISTS `tbl_rank`;

CREATE TABLE IF NOT EXISTS `tbl_language`(
   `id` INT UNSIGNED AUTO_INCREMENT,
   `name` VARCHAR(64) NOT NULL,
   `url` VARCHAR(128) NOT NULL,
   `founded_at` DATE,
   PRIMARY KEY ( `id` )
);

CREATE TABLE IF NOT EXISTS `tbl_rank`(
   `id` INT UNSIGNED AUTO_INCREMENT,
   `name` VARCHAR(64) NOT NULL,
   `month` VARCHAR(7) NOT NULL,
   `rank` TINYINT NOT NULL,
   `rate` VARCHAR(32) NOT NULL,
   PRIMARY KEY ( `id` )
);

INSERT INTO `tbl_language` VALUES
    (1,'Python','https://www.twle.cn','1991-2-20'),
    (2,'PHP','http://www.php.net','1994-1-1'),
    (3,'Ruby','https://www.ruby-lang.org/','1996-12-25');

INSERT INTO `tbl_rank` VALUES
    (1, 'Python','2018-04',4,'5.083%'),
    (2, 'PHP','2018-04',6,'4.218%'),
    (3, 'Ruby','2018-04',11,'2.018%'),
    (4, 'Java','2018-04',1,'15.777%'),
    (5, 'Python','2018-03',4,'5.869%'),
    (6, 'PHP','2018-03',7,'4.010%'),
    (7, 'Ruby','2018-03',12,'2.744%'),
    (8, 'Java','2018-03',1,'14.941'),
    (9, 'Python','2018-02',4,'5.168%'),
    (10, 'PHP','2018-02',7,'3.420%'),
    (11, 'Ruby','2018-02',10,'2.534%'),
    (12, 'Java','2018-02',1,'14.988%');

tbl_language 表中的数据如下

+----+--------+----------------------------+------------+
| id | name   | url                        | founded_at |
+----+--------+----------------------------+------------+
|  1 | Python | https://www.twle.cn        | 1991-02-20 |
|  2 | PHP    | http://www.php.net         | 1994-01-01 |
|  3 | Ruby   | https://www.ruby-lang.org/ | 1996-12-25 |
+----+--------+----------------------------+------------+

tbl_rank 表中的数据如下

+----+--------+---------+------+---------+
| id | name   | month   | rank | rate    |
+----+--------+---------+------+---------+
|  1 | Python | 2018-04 |    4 | 5.083%  |
|  2 | PHP    | 2018-04 |    6 | 4.218%  |
|  3 | Ruby   | 2018-04 |   11 | 2.018%  |
|  4 | Java   | 2018-04 |    1 | 15.777% |
|  5 | Python | 2018-03 |    4 | 5.869%  |
|  6 | PHP    | 2018-03 |    7 | 4.010%  |
|  7 | Ruby   | 2018-03 |   12 | 2.744%  |
|  8 | Java   | 2018-03 |    1 | 14.941  |
|  9 | Python | 2018-02 |    4 | 5.168%  |
| 10 | PHP    | 2018-02 |    7 | 3.420%  |
| 11 | Ruby   | 2018-02 |   10 | 2.534%  |
| 12 | Java   | 2018-02 |    1 | 14.988% |
+----+--------+---------+------+---------+

然后使用下面的语句创建一个交叉连接查询

SELECT tbl_language.id,tbl_language.name,tbl_rank.rank 
FROM tbl_language 
CROSS JOIN tbl_rank;

输出结果如下

id          name        rank      
----------  ----------  ----------
1           Python      4         
1           Python      6         
1           Python      11        
1           Python      1         
1           Python      4         
1           Python      7         
1           Python      12        
1           Python      1         
1           Python      4         
1           Python      7         
1           Python      10        
1           Python      1         
2           PHP         4         
2           PHP         6         
2           PHP         11        
2           PHP         1         
2           PHP         4         
2           PHP         7         
2           PHP         12        
2           PHP         1         
2           PHP         4         
2           PHP         7         
2           PHP         10        
2           PHP         1         
3           Ruby        4         
3           Ruby        6         
3           Ruby        11        
3           Ruby        1         
3           Ruby        4         
3           Ruby        7         
3           Ruby        12        
3           Ruby        1         
3           Ruby        4         
3           Ruby        7         
3           Ruby        10        
3           Ruby        1 

SQLite 基础教程

关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

  简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2022 简单教程 twle.cn All Rights Reserved.