Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.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中更新表_Sql_Postgresql - Fatal编程技术网

如何在postgresql中更新表

如何在postgresql中更新表,sql,postgresql,Sql,Postgresql,我有以下格式的表格: time year month day hour area state1 state3 2015-04-01 00:00:00 2015 4 1 0 1 100 300 2015-04-01 00:00:00 2015 4 1 0 2 300 300 2015-04-01 00:00:0

我有以下格式的表格:

time                  year   month   day   hour   area   state1    state3 
2015-04-01 00:00:00   2015     4      1      0      1     100        300
2015-04-01 00:00:00   2015     4      1      0      2     300        300
2015-04-01 00:00:00   2015     4      1      0      3     500        600
2015-04-01 01:00:00   2015     4      1      0      1     600        900
2015-04-01 01:00:00   2015     4      1      0      2     100        300
2015-04-01 01:00:00   2015     4      1      0      3     100        600 
2015-04-01 02:00:00   2015     4      1      0      2     900        300
2015-04-01 02:00:00   2015     4      1      0      3     300        900
2015-04-01 02:00:00   2015     4      1      0      1     100        300
.......................
...........
........
2015-04-30 23:00:00   2015     4      30      23    3     100        300
问题是在2015-04-10 00:00:00之后,我所有的记录在状态1和状态3中都有0。所以我想用150到300之间的随机数更新这些记录

我有一个返回随机数的函数

SELECT random_between(1,100)
FROM generate_series(1,5);

如何在2015-04-10 00:00:00之后使用我的功能更新state1和state3。非常感谢您的帮助。

以下面的会话为例。这是一个非常简单的例子,说明你正在尝试做什么,也许它可以适应你的情况。我使用了直接转换和函数:

> createdb test
> psql -d test
psql (9.4.9)
Type "help" for help.

test=# create table mytest(id integer, cola integer,colb integer);
CREATE TABLE
test=# select * from mytest;
 id | cola | colb 
----+------+------
(0 rows)

test=# insert into mytest values(1,0,0);
INSERT 0 1
test=# insert into mytest values(2,0,0);
INSERT 0 1
test=# insert into mytest values(3,0,0);
INSERT 0 1
test=# insert into mytest values(4,0,0);
INSERT 0 1
test=# select * from mytest;
 id | cola | colb 
----+------+------
  1 |    0 |    0
  2 |    0 |    0
  3 |    0 |    0
  4 |    0 |    0
(4 rows)

test=# update mytest set cola = cast(((random()*150)+150) as Integer) where id >2;
UPDATE 2
test=# select * from mytest;
 id | cola | colb 
----+------+------
  1 |    0 |    0
  2 |    0 |    0
  3 |  178 |    0
  4 |  198 |    0
(4 rows)

test=# create function myrand()
test-# returns integer as $i$
test$# declare i integer;
test$# begin
test$# return cast(((random()*150)+150) as Integer);
test$# end; 
test$# $i$ language plpgsql;
CREATE FUNCTION

test=# update mytest set colb = myrand() where id >2;
UPDATE 2
test=# select * from mytest;
 id | cola | colb 
----+------+------
  1 |    0 |    0
  2 |    0 |    0
  3 |  178 |  201
  4 |  198 |  291
(4 rows)

以下面的课程为例。这是一个非常简单的例子,说明你正在尝试做什么,也许它可以适应你的情况。我使用了直接转换和函数:

> createdb test
> psql -d test
psql (9.4.9)
Type "help" for help.

test=# create table mytest(id integer, cola integer,colb integer);
CREATE TABLE
test=# select * from mytest;
 id | cola | colb 
----+------+------
(0 rows)

test=# insert into mytest values(1,0,0);
INSERT 0 1
test=# insert into mytest values(2,0,0);
INSERT 0 1
test=# insert into mytest values(3,0,0);
INSERT 0 1
test=# insert into mytest values(4,0,0);
INSERT 0 1
test=# select * from mytest;
 id | cola | colb 
----+------+------
  1 |    0 |    0
  2 |    0 |    0
  3 |    0 |    0
  4 |    0 |    0
(4 rows)

test=# update mytest set cola = cast(((random()*150)+150) as Integer) where id >2;
UPDATE 2
test=# select * from mytest;
 id | cola | colb 
----+------+------
  1 |    0 |    0
  2 |    0 |    0
  3 |  178 |    0
  4 |  198 |    0
(4 rows)

test=# create function myrand()
test-# returns integer as $i$
test$# declare i integer;
test$# begin
test$# return cast(((random()*150)+150) as Integer);
test$# end; 
test$# $i$ language plpgsql;
CREATE FUNCTION

test=# update mytest set colb = myrand() where id >2;
UPDATE 2
test=# select * from mytest;
 id | cola | colb 
----+------+------
  1 |    0 |    0
  2 |    0 |    0
  3 |  178 |  201
  4 |  198 |  291
(4 rows)

谢谢你的详细解释谢谢你的详细解释