postgresql 如何使用具有许多不同值参数运行postgre sql查询?

c2e8gylq  于 2023-04-05  发布在  PostgreSQL
关注(0)|答案(1)|浏览(132)

所以如果我有一些简单插入查询,

INSERT INTO user_test_score
(test_id, date_occured, number_of_questions, user_id, score)
VALUES (123,'2023-03-30', 100, $1, $2)

我可以用(user_id,score)的数组在dbeaver中运行它吗?
我所做的是先用一些程序生成所有的查询
谢谢
bind参数只接受单个值,因此它传递数组,而不是值

cuxqih21

cuxqih211#

unnest可用于将数组转换为表

select some_table_name.*
from unnest(Array[1,2,3], Array[95,88,100]) as some_table_name(user_id,score);

然后插入查询可以写成

insert into user_test_score (test_id, date_occured, number_of_questions, user_id, score)
select
  123 as test_id,
  '2023-03-30' as date_occured,
  100 as number_of_questions,
  user_id_score_table.user_id,
  user_id_score_table.score
from
  unnest(Array[1,2,3], Array[95,88,100]) as user_id_score_table(user_id,score);

相关问题