SQL:一次插入多行

SQL:一次插入多行,sql,postgresql,Sql,Postgresql,我有这样一个值数组(restaurant\u id不是主键): 每天一次 我想将它们保存为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语句,还是可以在一个语句中循环并保存所有语句 使用循环的查询是什么

我有这样一个值数组(
restaurant\u id
不是主键):

每天一次

我想将它们保存为
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 (?, ?, ?, ? ?),
        (?, ?, ?, ? ?),
        (?, ?, ?, ? ?),
        (?, ?, ?, ? ?),
        (?, ?, ?, ? ?),
        (?, ?, ?, ? ?),
        (?, ?, ?, ? ?)

但是有更好的方法吗?

您可以发布一个插入。我建议使用以下参数:

INSERT INTO schedules (restaurant_id, day_of_week, from_time, to_time, is_open)
    VALUES (?, ?, ?, ? ?),
           (?, ?, ?, ? ?),
           . . .
    RETURNING *;

您可以发出一个插入。我建议使用以下参数:

INSERT INTO schedules (restaurant_id, day_of_week, from_time, to_time, is_open)
    VALUES (?, ?, ?, ? ?),
           (?, ?, ?, ? ?),
           . . .
    RETURNING *;
插入用户(id、姓名、年龄) 价值观 (1,'Talha',22), (2,'约翰',41岁), (3,'威廉',32岁)

我想这会起作用。

插入用户(id、姓名、年龄) 价值观 (1,'Talha',22), (2,'约翰',41岁), (3,'威廉',32岁)


我想这是可行的。

如果所有其他值都是常量(或可从运行变量派生),则可以使用
generate\u series()



生成_系列的文档

如果所有其他值都是常量(或可从运行变量派生),则可以使用
generate\u series()



生成_系列的文档
凯文·阿米拉诺夫, 您可以使用mysql的存储过程来实现这一点

开始 因为我在1。。100000 环 插入用户值(i,'Talha',20+i); 端环; 犯罪 结束

凯文·阿米拉诺夫, 您可以使用mysql的存储过程来实现这一点

开始 因为我在1。。100000 环 插入用户值(i,'Talha',20+i); 端环; 犯罪
结束

如果这些输入值实际上是JSON数组的一部分,则可以直接使用:

INSERT INTO schedules (restaurant_id, day_of_week, from_time, to_time, is_open) 
select (v ->> 'restaurant_id')::int, 
       (v ->> 'day_of_week')::int,
       (v ->> 'from_time')::time,
       (v ->> 'to_time')::time,
       (v ->> 'is_open')::boolean
from jsonb_array_elements('
[
  {"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" }
]'::jsonb) as t(v);

当然,您需要使用适当的参数替换硬编码的字符串值,例如来自jsonb_数组_元素(cast(?as jsonb))
如果这些输入值实际上是JSON数组的一部分,您可以直接使用:

INSERT INTO schedules (restaurant_id, day_of_week, from_time, to_time, is_open) 
select (v ->> 'restaurant_id')::int, 
       (v ->> 'day_of_week')::int,
       (v ->> 'from_time')::time,
       (v ->> 'to_time')::time,
       (v ->> 'is_open')::boolean
from jsonb_array_elements('
[
  {"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" }
]'::jsonb) as t(v);

当然,您需要使用适当的参数替换硬编码的字符串值,例如来自jsonb_数组_元素(cast(?as jsonb))

啊,是的!因此,为每一行显式设置值比在数组中循环更好?啊,是的!因此,显式地为每一行设置值比在数组中循环要好吗?是的,我只是想知道我是否可以循环并避免写入
(1,'Talha',22),
7次……Kevin Amiranoff,你可以使用mysql的存储过程来实现这一点。从1.开始为我做准备。。100000循环插入用户值(i,'Talha',20+i);端环;犯罪结束;是的,我只是想知道我是否可以循环并避免写
(1,'Talha',22),
7次……Kevin Amiranoff,你可以使用mysql的存储过程来实现这一点。从1.开始为我做准备。。100000循环插入用户值(i,'Talha',20+i);端环;犯罪结束;我假设您正在使用某种编程语言,正如您提到的“带循环的查询是什么?”我假设您正在使用某种编程语言,正如您提到的“带循环的查询是什么?”参考此链接,它可能有助于参考此链接,这可能是postgres特有的功能吗?是的。Generate_series()是postgres的扩展。这是特定于postgres的函数吗?是的。Generate_series()是postgres的扩展。