两个 SELECT 语句之间的 MySQL 笛卡尔积

发布于 2024-10-08 11:10:08 字数 189 浏览 0 评论 0原文

我想在两个 SELECT 语句之间执行笛卡尔积,因为

SELECT 1, 2     INNER JOIN     SELECT 3, 4 ;

我希望结果是 (1,2) 与 (3,4) 的每个组合,例如:

1  3
2  3
1  4
2  4

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 be every combination of (1,2) with (3,4), like:

1  3
2  3
1  4
2  4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

疧_╮線 2024-10-15 11:10:08

如果您指定的表没有任何 JOIN ON 子句或 WHERE 子句中的等式/条件,您将获得您正在寻找的笛卡尔积。

SELECT table1.field1, table2.field2
FROM table1, table2

会给你你所要求的。更明确地显示它...

SELECT * FROM table1;
+--------+
| field1 |
+--------+
|      1 |
|      2 |
+--------+

SELECT * FROM table2;
+--------+
| field2 |
+--------+
|      3 |
|      4 |
+--------+

SELECT table1.field1, table2.field2 FROM table1, table2;
+--------+--------+
| field1 | field2 |
+--------+--------+
|      1 |      3 |
|      2 |      3 |
|      1 |      4 |
|      2 |      4 |
+--------+--------+

If you specify your tables with out any JOIN ON clause or equalities/conditionins in the WHERE clause you'll get the catesian product you're looking for.

SELECT table1.field1, table2.field2
FROM table1, table2

will give you what you're asking for. Showing it more explicitly...

SELECT * FROM table1;
+--------+
| field1 |
+--------+
|      1 |
|      2 |
+--------+

SELECT * FROM table2;
+--------+
| field2 |
+--------+
|      3 |
|      4 |
+--------+

SELECT table1.field1, table2.field2 FROM table1, table2;
+--------+--------+
| field1 | field2 |
+--------+--------+
|      1 |      3 |
|      2 |      3 |
|      1 |      4 |
|      2 |      4 |
+--------+--------+
稚气少女 2024-10-15 11:10:08

您可以使用 CROSS JOIN 子句

SELECT MyTable1.Col1, MyTable2.Col2
FROM MyTable1
CROSS JOIN MyTable2

,其中 MyTable1 有两行包含 1 和 2; MyTable2 有两行,其中包含 3 和 4。

You can use the CROSS JOIN clause

SELECT MyTable1.Col1, MyTable2.Col2
FROM MyTable1
CROSS JOIN MyTable2

where MyTable1 has two rows containing 1 and 2; and MyTable2 has two rows containing 3 and 4.

卖梦商人 2024-10-15 11:10:08
select v1, v2
from
  (select 1 as v1 union
   select 2) t1,
  (select 3 as v2 union
   select 4) t2

甚至更简单:

select *
from
  (select 1 union
   select 2) t1,
  (select 3 union
   select 4) t2
select v1, v2
from
  (select 1 as v1 union
   select 2) t1,
  (select 3 as v2 union
   select 4) t2

or even simpler:

select *
from
  (select 1 union
   select 2) t1,
  (select 3 union
   select 4) t2
┊风居住的梦幻卍 2024-10-15 11:10:08
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)

删除单词 join 会得到完全相同的结果。

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;
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;
过期情话 2024-10-15 11:10:08

使用这个你的格式并不像你说的那样
A(1,2) 和 B(3,4)
那么交叉连接将像这样执行:

SELECT Table1.Col1, Table2.Col2
FROM Table1
CROSS JOIN Table2

(A*B)= (1,3),(1,4),(2,3),(2,4)< /代码>

With the using this your format is not as you are saying
A(1,2) and B(3,4)
then the cross join will perform it like this:

SELECT Table1.Col1, Table2.Col2
FROM Table1
CROSS JOIN Table2

(A*B)= (1,3),(1,4),(2,3),(2,4)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文