在postgres中展平嵌套记录(Flattening nested record in postgres)

编程入门 行业动态 更新时间:2024-10-28 16:22:39
在postgres中展平嵌套记录(Flattening nested record in postgres)

如何在外部选择中展平foo列(在PostgreSQL中)?

WITH RECURSIVE t AS ( SELECT row(d.*) as foo FROM some_multicolumn_table as d UNION ALL SELECT foo FROM t WHERE random() < .5 ) SELECT foo FROM t

我想要的是输出外部选择中some_multicolumn_table所有列(水平,即作为多行的一行),而不仅仅是单个“记录”列。

怎么做?

How to flatten the foo column in the outer select (in PostgreSQL)?

WITH RECURSIVE t AS ( SELECT row(d.*) as foo FROM some_multicolumn_table as d UNION ALL SELECT foo FROM t WHERE random() < .5 ) SELECT foo FROM t

What I want is to output all the columns (horizontally, i.e. as a row of multiple columns) of some_multicolumn_table in the outer select, not just a single "record" column.

How to do that?

最满意答案

你不需要那里的ROW构造函数,所以你可以使用(foo).*扩展记录(foo).* :

WITH RECURSIVE t AS ( SELECT d as foo FROM some_multicolumn_table as d UNION ALL SELECT foo FROM t WHERE random() < .5 ) SELECT (foo).* FROM t;

虽然这个查询可以简单地写成:

WITH RECURSIVE t AS ( SELECT d.* FROM some_multicolumn_table as d UNION ALL SELECT t.* FROM t WHERE random() < .5 ) SELECT * FROM t;

我建议尽量保持简单。 但我认为这只是一个例证。

You don't need the ROW constructor there, and so you can expand the record by using (foo).*:

WITH RECURSIVE t AS ( SELECT d as foo FROM some_multicolumn_table as d UNION ALL SELECT foo FROM t WHERE random() < .5 ) SELECT (foo).* FROM t;

Although this query could be simple written as:

WITH RECURSIVE t AS ( SELECT d.* FROM some_multicolumn_table as d UNION ALL SELECT t.* FROM t WHERE random() < .5 ) SELECT * FROM t;

And I recommend trying to keep it as simple as possible. But I'm assuming it was just an exemplification.

更多推荐

本文发布于:2023-08-05 16:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1436059.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   中展平   postgres   record   nested

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!