我正在尝试执行以下操作。
我想创建一个包含某些事物之间所有可能关系的列表。
例如。有 Mary、Alice、June、Cindy、Elizabeth、Betty、Jax
我想为这样的列表创建所有可能的组合:
- 玛丽,爱丽丝
- 玛丽,六月
- 玛丽·辛迪
- 玛丽,贾克斯
- 玛丽、爱丽丝、琼
- 玛丽、爱丽丝、辛迪
- 玛丽、爱丽丝、伊丽莎白 ...
- 玛丽、爱丽丝、贾克斯
- 玛丽、琼、辛迪
- 玛丽、琼、伊丽莎白 ...
- 玛丽、琼、贾克斯
- 玛丽、辛迪、伊丽莎白
- 玛丽、辛迪、贝蒂
- 玛丽、辛迪、贾克斯 ...
- 玛丽、爱丽丝、琼、辛迪
- 玛丽、爱丽丝、琼、伊丽莎白
- 玛丽、爱丽丝、琼、贝蒂 ...
- 玛丽、爱丽丝、琼、辛迪、伊丽莎白
- 玛丽、爱丽丝、琼、辛迪、贝蒂
有人知道在 SQL、Access 或 C# 中执行此操作的方法吗?如果有另一种语言可以在数据库上使用,我将不胜感激!
谢谢, 玛丽亚
最佳答案
您可能喜欢许多现代数据库服务器为此使用的递归查询。
ACCESS 不是其中之一:(
以下是 postres 的示例
postgres=# with RECURSIVE y1(b,c,d) as (
postgres(# with x1(a) as (
postgres(# values('a')
postgres(# union all
postgres(# values ('b')
postgres(# union all
postgres(# values ('c')
postgres(# union all
postgres(# values ('d')
postgres(# )
postgres(# select a,a,1
postgres(# from x1
postgres(# union all
postgres(# select a||b,a,d+1
postgres(# from x1
postgres(# join y1 on (a < c)
postgres(# )
postgres-# select *
postgres-# from y1;
b | c | d
------+---+---
a | a | 1
b | b | 1
c | c | 1
d | d | 1
ab | a | 2
ac | a | 2
ad | a | 2
bc | b | 2
bd | b | 2
cd | c | 2
abc | a | 3
abd | a | 3
acd | a | 3
bcd | b | 3
abcd | a | 4
(15 rows)
postgres=#
关于sql - 创建所有可能组合的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9134650/