Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在postgresql的一列中添加数字范围(1..5)_Sql_Postgresql - Fatal编程技术网

在postgresql的一列中添加数字范围(1..5)

在postgresql的一列中添加数字范围(1..5),sql,postgresql,Sql,Postgresql,我想在列中插入数字1到5的范围 INSERT INTO public.npc(name, gold, info, id, quest_level) VALUES ('test',200,'do_that',1,(1..5) 我假设您的quest\u级别的数据类型为int[] CREATE TABLE public.npc( name text, gold int, info text, id int, quest_level int[] );

我想在列中插入数字1到5的范围

INSERT INTO public.npc(name, gold, info, id, quest_level)
VALUES ('test',200,'do_that',1,(1..5)

我假设您的
quest\u级别
的数据类型为
int[]

 CREATE TABLE public.npc(
    name text, 
    gold int, 
    info text, 
    id int, 
    quest_level int[]
);
在这种情况下,您可以像这样插入值:

INSERT INTO public.npc (name, gold, info, id, quest_level) VALUES 
('test',200,'do_that',1, (SELECT array_agg(g.i) 
                          FROM generate_series(1,5) as g(i)));
如果
quest\u level
有另一种数据类型,例如
text
,则需要强制转换:

 CREATE TABLE public.npc(
    name text, 
    gold int, 
    info text, 
    id int, 
    quest_level text
 );

INSERT INTO public.npc (name, gold, info, id, quest_level) VALUES 
('test',200,'do_that',1, (SELECT array_agg(g.i) :: text
                          FROM generate_series(1,5) as g(i)));
如果要引入5行,则:

INSERT INTO public.npc(name, gold, info, id, quest_level)
    SELECT 'test', 200, 'do_that', 1, gs.n
    FROM generate_series(1, 5) gs(n);

它应该是什么数据类型?是要存储该范围内的所有值,还是仅存储该范围的开始和结束?如果是后者,请使用