如何生成 m 列 n 行的排列或组合?

发布于 2024-10-01 11:49:01 字数 350 浏览 0 评论 0原文

有谁知道如何从 n 行表中创建一个包含 m 列的表,其中每行列中的值代表原始表中值的不同组合或排列?

例如,原始表有 1 列 (number_value) 和 3 (n=3) 行:

1
2
3

包含两个值 (m = 2) 的组合(顺序无关紧要)的表如下

number1, number2
1,2
1,3
2,3

:排列如下:

number1, number2
1, 2
2, 1
1, 3
3, 1
2, 3
3, 2

行的顺序并不重要。

先感谢您!

Does anyone know how to create a table with m columns from table of n rows where the values in columns of each row represent a different combination or permutation of values from the original table?

For example the original table has 1 column (number_value) with 3 (n=3) rows:

1
2
3

The table which contains combinations (the order doesn't matter) of two values (m = 2) would be the following:

number1, number2
1,2
1,3
2,3

and the table of permutations would be the following:

number1, number2
1, 2
2, 1
1, 3
3, 1
2, 3
3, 2

The order of rows doesn't matter.

Thank you in advance!

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

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

发布评论

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

评论(1

怕倦 2024-10-08 11:49:01

组合:

SELECT T1.x, T2.x
FROM your_table T1
JOIN your_table T2
ON T1.x < T2.x

排列:

SELECT T1.x, T2.x
FROM your_table T1
JOIN your_table T2
ON T1.x != T2.x

我假设原始表中的值是唯一的。

要推广较大的 m 值,您需要添加更多连接。

Combinations:

SELECT T1.x, T2.x
FROM your_table T1
JOIN your_table T2
ON T1.x < T2.x

Permutations:

SELECT T1.x, T2.x
FROM your_table T1
JOIN your_table T2
ON T1.x != T2.x

I am assuming that the values in the original table are unique.

To generalize for larger values of m you need to add more joins.

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