【发布时间】:2019-06-15 12:04:02
【问题描述】:
我有一个这样的值数组(restaurant_id 不是主键):
[
{restaurant_id:1, day_of_week:0, from_time: "12:00", to_time: "14:00", is_open:false },
{restaurant_id:1, day_of_week:1, from_time: "12:00", to_time: "14:00", is_open:true },
{restaurant_id:1, day_of_week:2, from_time: "12:00", to_time: "14:00", is_open:true },
...
]
每天一个条目。
我想将它们中的每一个都保存为 PostgreSQL 数据库中的新行。
我有一个插入查询:
INSERT INTO schedules (restaurant_id, day_of_week, from_time, to_time, is_open) VALUES ($1, $2, $3, $4, $5) RETURNING schedules;
我应该执行 7 个 INSERT 语句还是可以循环并在一个语句中保存所有语句?
循环的查询是什么?
编辑:
所以我可以按照建议在一个查询中做这样的事情:
VALUES (?, ?, ?, ? ?),
(?, ?, ?, ? ?),
(?, ?, ?, ? ?),
(?, ?, ?, ? ?),
(?, ?, ?, ? ?),
(?, ?, ?, ? ?),
(?, ?, ?, ? ?)
但是有没有更好的方法呢?
【问题讨论】:
-
我假设您也使用了某种编程语言,正如您提到的那样“循环的查询是什么?”
标签: sql postgresql