INSERT OVERWRITE TABLE但更改单个字段

huangapple go评论66阅读模式
英文:

INSERT OVERWRITE TABLE but change single field

问题

我想更新表中所有项目的字段,如果字段的值为空字符串。

为了了解情况,我可以使用以下查询来获取需要更新的项目数量:

select count(*) from ddb_table where someField = "";

然后,我想要更新这个表中的项目,其中 someField 的值为空字符串。例如,我想要将 someField 更新为 "foo",其中字段为空。

我想象中的 INSERT OVERWRITE 查询可能如下所示:

insert overwrite ddb_table select field1, field2, field3, field4, "foo" as someField FROM ddb_table where someField = "";

换句话说,我希望保留表中每个项目的所有原始值,除了 someField,我希望将其更新为 "foo"。这个更新只会在 someField 为空的项目上发生。

我该如何做呢?

英文:

I am looking to update a field for all items in a table if the value of the field is an empty string.

For context, I can get a count of the number of items that I need to update with this query:

select count(*) from ddb_table where someField = "";

I then want to update the items in this table where someField is an empty string. For example, I want to update someField to "foo" where the field is blank.

I imagine the INSERT OVERWRITE query would look something like this:

insert overwrite ddb_table select field1, field2, field3, field4, someField = "foo" FROM ddb_table where someField = "";

In other words, I'm wanting to "keep" all the original values for each item in the table, except for someField, which I want to update to "foo". This update would only occur on items where someField is blank.

How do I do this?

答案1

得分: 0

对于我的原始查询:

insert overwrite table ddb_table select field1, field2, field3, field4, someField = "foo" FROM ddb_table where someField = "";

结果是someField似乎取了field4中的值,而field4会被设为FALSE

然后我尝试了:

insert overwrite table ddb_table select field1, field2, field3, field4, "foo" as someField FROM ddb_table where someField = "";

这稍微好一点,除了field4会被设为"foo",而someField会被设为之前在field4中的值。

接着我尝试了:

insert overwrite table ddb_table select field1, field2, field3, "foo" as someField, field4 FROM ddb_table where someField = "";

这个对我有效。出于某种原因,将我想要更改的字段从字段列表的末尾移动到字段列表中间似乎让它起作用。

英文:

For my original query:

insert overwrite table ddb_table select field1, field2, field3, field4, someField = "foo" FROM ddb_table where someField = "";

The result was that someField appeared to take the value that was in field4, and field4 would get set to FALSE.

I then tried:

insert overwrite table ddb_table select field1, field2, field3, field4, "foo" as someField FROM ddb_table where someField = "";

This was slightly better, except that field4 would be set to "foo", and someField would be set to the value that was previously in field4.

Then I tried:

insert overwrite table ddb_table select field1, field2, field3, "foo" as someField, field4 FROM ddb_table where someField = "";

This one worked for me. For some reason moving the field that I want to change from the end of the list of fields to somewhere in the middle of the list of fields seemed to make it work.

huangapple
  • 本文由 发表于 2023年7月3日 13:57:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76602155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定