简体   繁体   English

在 Supabase 数据库(postgres)中的单个查询中更新多行

[英]Update multiple rows in a single query in a Supabase Database (postgres)

I looking to update multiple rows via a single query in Supabase.我希望通过 Supabase 中的单个查询来更新多行。 I'm struggling to find an example of how to do it online.我正在努力寻找如何在线进行操作的示例。

For this example here is how the data exists the database's 'food' table:对于此示例,这里是数据如何存在于数据库的“食物”表中:

id ID title标题 qty数量
1 1个 Apple苹果 10 10
2 2个 Banana香蕉 8 8个
3 3个 Mango芒果 4 4个

What i would like to do is update the 3 qty fields in the single query (this code doesn't work but should give you an idea of what i am after).我想做的是更新单个查询中的 3 个数量字段(此代码不起作用,但应该让您了解我在做什么)。

const { data: item_data, error: item_error } = await supabase
   .from('food')
   .update({ qty: 11, }).eq('id', '1')
   .update({ qty: 9, }).eq('id', '2')
   .update({ qty: 6, }).eq('id', '3')

You can do it with an upsert() :您可以使用upsert()来做到这一点:

const { data, error } = await supabase
  .from('food')
  .upsert([{id: 1, qty: 11}, {id: 2, qty: 9}, {id: 3, qty: 6}])

Additionally you can also do it with a SQL function as mentioned here .此外,您还可以使用此处提到的 SQL function 来完成此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM