【问题标题】:Easily output the hstore format of a table row轻松输出表格行的hstore格式
【发布时间】:2012-01-10 20:12:03
【问题描述】:

有没有比使用更好的方法将表格的一行转换为 hstore 格式

SELECT hstore(ARRAY['col1','col2','col3'], ARRAY[col1::text, col2::text, col3::text]) FROM tbl;

它有效,但我认为必须有比输入每一列更好的方法。 hstore 采用记录类型作为输入,但我不知道如何将单行生成查询输入到函数中并使其满意。 Postgres 版本 9.0.4。

【问题讨论】:

    标签: postgresql hstore


    【解决方案1】:

    是的 - 您可以使用 hstore() 函数将 row 转换为 hstore 类型。

    SELECT hstore(tbl.*) FROM tbl;
    

    为我工作:

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

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

    【讨论】:

    • 啊!我没想到把表名放在那里。我刚试过 SELECT hstore(*) FROM tbl。谢谢!
    • 由于某种原因,星号 (*) 的解析方式与 (foo.*) 不同。奇怪,但这就是它的工作原理。
    猜你喜欢
    • 2018-04-19
    • 2012-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    相关资源
    最近更新 更多