sql - 创建所有可能组合的列表

标签 sql database combinations

我正在尝试执行以下操作。

我想创建一个包含某些事物之间所有可能关系的列表。

例如。有 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/

相关文章:

sql - 将日期插入 Oracle 11g 数据库时遇到问题

mysql - 为什么 POINT() 被存储为 ??*??E@z?3M??Q?在 mysql 表中

javascript - 向 javascript 数组的 n 种组合添加计数器

c# - 如何仅从字符串数组中选择一个随机字符串一次

mysql - 选择左连接值不同的行mysql

mysql - 升级到 Rails 4.2.0 : string literals in where conditions wrapped into quotation marks

database - Cassandra DB 向多个表中插入数据

python - 具有 "Gray code"类似属性的 n 个项目的 k 组合

mysql - 使用 sum 和 where

MYSQL在Insert中使用子查询结果