如何在 BigQuery 中使用 SELECT FROM VALUES 编写 MERGE 查询?

问题描述 投票:0回答:2

我想使用

MERGE
数据库中的文字值编写
BigQuery
查询。 我尝试了这个:https://stackoverflow.com/a/35659674/5659025(SQL Server 版本),但 BigQuery 返回错误:“找不到表值函数”

是否可以在

BigQuery
中实现以及如何实现?

sql merge google-bigquery
2个回答
0
投票

经过我们在评论部分的讨论,我可以建议您两个选择:

首先,使用 INSERT 语句。根据文档:

当您想要向表中添加新行时,请使用 INSERT 语句。

因此,您修改源表中的行。我使用了虚拟数据来重现它。我的源数据库是:

在 BigQuery UI 中,语法如下:

insert `source_table` (email, content)
select "[email protected]" as email, "add something" as content

insert `source_table` (email, content)
select "[email protected]" , "add something" 

请注意,我使用了双引号,因为我要添加的字段是一个 String。输出如下:

第二个选项是使用UNION ALL。我想指出的是,使用此选项您不会更新或修改源表,而是创建一个添加了新数据的视图。语法如下:

SELECT '[email protected]' , "add something 2" UNION ALL
--#if you want to add more rows to your view, you will need to write another 
--#select '[email protected]', "something " UNION ALL
SELECT * FROM  `test-proj-261014.sample.foobar`

希望有帮助。


0
投票

给定一个现有的表:

create table if not exists test.my_table (
  name string,
  value int64
);

insert into test.my_table (name, value)
values ('alex', 10);

您可以使用

UNION ALL
和许多
select
语句合并新值。

merge into test.my_table t
using (
  (select 'alex' name, 15 value)
  union all
  (select 'dimitri' name, 20 value)
) s
on t.name = s.name
when matched then update set t.value = s.value
when not matched then insert row

您需要

select
来命名列,您需要命名列以将现有表连接到值。

© www.soinside.com 2019 - 2024. All rights reserved.