MySql在from子句中连接两个没有子查询的select语句

时间:2016-09-21 07:47:01

标签: mysql join

如何在不使用from子句中的子查询的情况下加入这两个选择?

SELECT 0 a UNION SELECT 1 a UNION SELECT 2 UNION SELECT 3     UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7     UNION SELECT 8 UNION SELECT 9;

SELECT 0 b UNION SELECT 10 UNION SELECT 20     UNION SELECT 30 UNION SELECT 40 UNION SELECT 50 UNION SELECT 60      UNION SELECT 70 UNION SELECT 80 UNION SELECT 90;

结果应为100行:

每一个(0,1,2,3,4,5,6,7,8,9)加入每个b(0,10,20,30,40,50,60,70,80,90) )

我需要这个,因为如果from子句中有子查询,我就无法创建视图......

谢谢!

1 个答案:

答案 0 :(得分:2)

使用CROSS JOIN

SELECT 
*
FROM 
( SELECT 0 a UNION SELECT 1 a UNION SELECT 2 UNION SELECT 3
    UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7
    UNION SELECT 8 UNION SELECT 9 ) AS A


CROSS JOIN

(
 SELECT 0 b UNION SELECT 10 UNION SELECT 20 
    UNION SELECT 30 UNION SELECT 40 UNION SELECT 50 UNION SELECT 60
     UNION SELECT 70 UNION SELECT 80 UNION SELECT 90
) AS B

注意:

如果表格An行,而表格Bm行,那么

A CROSS B将有m * n