MySQL cartesian product between two SELECT statements

前端 未结 5 1952
孤街浪徒
孤街浪徒 2020-12-03 12:16

I want to perform a cartesian product between two SELECT statements as

SELECT 1, 2     INNER JOIN     SELECT 3, 4 ;

I expect the result to

5条回答
  •  佛祖请我去吃肉
    2020-12-03 12:28

    mysql> select * from
        ->   (select 1 x union select 2 union select 3 union select 4) t
        ->   join
        ->   (select 1 y union select 2 union select 3) tt;
    +---+---+
    | x | y |
    +---+---+
    | 1 | 1 |
    | 1 | 2 |
    | 1 | 3 |
    | 2 | 1 |
    | 2 | 2 |
    | 2 | 3 |
    | 3 | 1 |
    | 3 | 2 |
    | 3 | 3 |
    | 4 | 1 |
    | 4 | 2 |
    | 4 | 3 |
    +---+---+
    12 rows in set (0.00 sec)
    

    Removing the word join gives exactly the same result.

    select * from
      (select 1 x union select 2 union select 3 union select 4) t,
      (select 1 y union select 2 union select 3) tt;
    

提交回复
热议问题