在 postgres 中显示 2 个字符的所有变体

Luc*_*man 1 postgresql

我将如何在 postgres 中做到这一点:

假设我有一个 x 和 ay,我想显示所有可能的组合:

xx
yy
xy
yx
Run Code Online (Sandbox Code Playgroud)

我怎样才能让 postgres 为我做这件事?

a_h*_*ame 7

with characters (c) as (
  select unnest(string_to_array('xy', null))
) 
select *
from characters c1
  cross join characters c2
Run Code Online (Sandbox Code Playgroud)

编辑:

显然这在 8.4 中不起作用,但以下内容应该:

with characters (c) as (
  select unnest(regexp_split_to_array('xy', ''))
) 
select *
from characters c1
  cross join characters c2
Run Code Online (Sandbox Code Playgroud)

感谢布鲁诺对此进行测试。

  • 我也无法让它与 8.4 一起工作,但是用 `regexp_split_to_array('xy', '')` 替换 `string_to_array('xy', null)` 是可行的。归功于 [this thread](http://archives.postgresql.org/pgsql-hackers/2009-07/msg01640.php),这可能与在较新版本中支持 `NULL` 作为分隔符有关。 (2认同)