选择没有表的硬编码值

我需要在不实际连接任何表的情况下运行一个 select。我只是有一组预定义的硬编码值需要循环:

foo
bar
fooBar

我想循环遍历这些值,我可以这样做:

select 'foo', 'bar', 'fooBar';

但是这样返回的结果是一行:

 ?column? | ?column? | ?column?
----------+----------+----------
foo      | bar      | fooBar
(1 row)

我正在使用 Postgreql。

58964 次浏览
select a
from (
values ('foo'), ('bar'), ('fooBar')
) s(a);

http://www.postgresql.org/docs/current/static/queries-values.html

Using unnest()

expand an array to a set of rows

select unnest(array['foo', 'bar', 'fooBar']);

demo

Postgres SQL:

For static data output as single row and multiple columns representation use following query:

select a,b,c from (values('foo','bar','fooBar')) s(a,b,c);

result of this SQL query:

enter image description here