Jak*_*son 3 postgresql supabase supabase-database
我希望通过 Supabase 中的单个查询更新多行。我正在努力寻找如何在线完成此操作的示例。
对于此示例,以下是数据如何存在于数据库的“food”表中:
| ID | 标题 | 数量 |
|---|---|---|
| 1 | 苹果 | 10 |
| 2 | 香蕉 | 8 |
| 3 | 芒果 | 4 |
我想做的是更新单个查询中的 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')
Run Code Online (Sandbox Code Playgroud)
您可以使用以下方法来做到这一点upsert():
const { data, error } = await supabase
.from('food')
.upsert([{id: 1, qty: 11}, {id: 2, qty: 9}, {id: 3, qty: 6}])
Run Code Online (Sandbox Code Playgroud)
此外,您还可以使用此处提到的 SQL 函数来完成此操作。