gpt4 book ai didi

mysql - PostgreSQL 和 MySQL 中 BigQuery STRUCT 的类比

转载 作者:行者123 更新时间:2023-11-29 14:27:51 24 4
gpt4 key购买 nike

MySQL 和 PostgreSQL 是否具有 Google BigQuery 的 STRUCT 类似物?

你可以在任何地方创建一个结构,这很巧妙

STRUCT(<value_1> as <field_1>, <value_2> as <field_2>, ...)

PostgreSQL 有CREATE TYPE,但似乎没有那么轻量级。

是否有或多或少的直接模拟?

例如,在 BigQuery 中,我们可以创建一个表,如下所示:

create or replace table my_dataset.t as (
select "John" as name, struct("US" as country, "New York" as city) as location union all
select "James" as name, struct("US" as country, "Seattle" as city) as location
);

国家和城市被组织成一个结构,这通常很方便。例如。我们可以通过运行列出所有位置

select location from my_dataset.t;

在 PostgreSQL 中,如果我们创建一个类型,这似乎是可行的。我的理解是否正确,否则就没有那么简单了?

最佳答案

在 PostgreSQL 中你可以使用:

SELECT * FROM (VALUES(1, 2)) AS tab(a, b);

a | b
---+---
1 | 2
(1 row)

您评论中的示例稍微复杂一些,但在 PostgreSQL 中没有什么是您做不到的:

WITH location AS (
SELECT 'US' AS country,
'New York' AS city
),
people AS (
SELECT 'John' AS name,
location
FROM location
)
SELECT (location).* FROM people;

关于mysql - PostgreSQL 和 MySQL 中 BigQuery STRUCT 的类比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55914305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com