I need to run a select without actually connecting to any table. I just have a predefined hardcoded set of values I need to loop over:
foo bar fooBar
And I want to loop through those values. I can do:
select 'foo', 'bar', 'fooBar';
But this returns it as one row:
?column? | ?column? | ?column? ----------+----------+---------- foo | bar | fooBar (1 row)
I am using Postgresql.
select without from , on the other hand, works on four of them. By now you might wonder why stand-alone values might be useful at all. As I implied above, it is more powerful than select without from because it is not limited to produce a single row. With select without from , you'd need to use union .
PostgreSQL SELECT statement syntaxIf you specify a list of columns, you need to place a comma ( , ) between two columns to separate them. If you want to select data from all the columns of the table, you can use an asterisk ( * ) shorthand instead of specifying all the column names.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With