轻松输出表行的hstore格式

Evi*_*t7x 6 postgresql hstore

是否有更好的方法将表格的行放入hstore格式而不是去

SELECT hstore(ARRAY['col1','col2','col3'], ARRAY[col1::text, col2::text, col3::text]) FROM tbl;
Run Code Online (Sandbox Code Playgroud)

它有效,但我认为必须有一个更好的方法,而不是键入每列.hstore采用记录类型进行输入,但我无法弄清楚如何将单行生成查询提供给函数并使其快乐.Postgres版本9.0.4.

fil*_*rem 10

是 - 您可以使用hstore()函数将行转换为hstore类型.

SELECT hstore(tbl.*) FROM tbl;
Run Code Online (Sandbox Code Playgroud)

适合我:

filip@filip=# select hstore(foo.*) from foo;
         hstore
------------------------
 "bar"=>"1", "baz"=>"2"
(1 row)
Run Code Online (Sandbox Code Playgroud)

http://www.postgresql.org/docs/9.0/static/hstore.html#HSTORE-FUNC-TABLE

  • 啊! 我没想到要把表名放在那里。我刚刚尝试了 SELECT hstore(*) FROM tbl。谢谢! (2认同)
  • 由于某种原因,星号“(*)”的解析方式与“(foo.*)”不同。很奇怪,但这就是它的工作原理。 (2认同)