Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.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 9.3:从另一个表更新表(订单列),获得相同的行值_Postgresql_Sql Update_Postgresql 9.1_Postgresql 9.3 - Fatal编程技术网

PostgreSQL 9.3:从另一个表更新表(订单列),获得相同的行值

PostgreSQL 9.3:从另一个表更新表(订单列),获得相同的行值,postgresql,sql-update,postgresql-9.1,postgresql-9.3,Postgresql,Sql Update,Postgresql 9.1,Postgresql 9.3,我需要帮助从Postgres Db中的另一个表更新表 长话短说,我们最终在数据库中得到了损坏的数据,现在我需要用另一个表中的值更新一个表 我有一个包含此数据的表格表格wfc: | step_id | command_id | commands_order | |---------|------------|----------------| | 1 | 1 | 0 | | 1 | 2 |

我需要帮助从Postgres Db中的另一个表更新表

长话短说,我们最终在数据库中得到了损坏的数据,现在我需要用另一个表中的值更新一个表

我有一个包含此数据的表格
表格wfc

| step_id | command_id | commands_order |
|---------|------------|----------------|
|       1 |          1 |              0 |
|       1 |          2 |              1 |
|       1 |          3 |              2 |
|       1 |          4 |              3 |
|       1 |          1 |              0 |
|       2 |          2 |              0 |
|       2 |          3 |              1 |
|       2 |          3 |              1 |
|       2 |          4 |              3 |
我想从另一个表中更新command_order列中的值,这样可以得到如下结果:

| step_id | command_id | commands_order|
|---------|------------|---------------|
|       1 |          1 |             0 |
|       1 |          2 |             1 |
|       1 |          3 |             2 |
|       1 |          4 |             3 |
|       1 |          1 |             4 |
|       2 |          2 |             0 |
|       2 |          3 |             1 |
|       2 |          3 |             2 |
|       2 |          4 |             3 |
这看起来很简单,但问题是要为相同的
command\u id
更新行,它在
commands\u顺序中写入相同的值

我尝试的SQL是:

UPDATE wfc 
SET commands_order = CAST(sq.input_step_id as INTEGER) 
FROM  ( 
SELECT wfp.step_id, wfp.input_command_id, wfp.input_step_id
from wfp 
order by wfp.step_id, wfp.input_step_id

   ) AS sq
WHERE  (wfc.step_id=sq.step_id AND wfc.command_id=CAST(sq.input_command_id as INTEGER));
SQL小提琴

我被这件事缠住了,请帮帮我


提前感谢。

假设您正在尝试按照创建行的顺序对行进行编号,并且只要您了解ctid将在更新时进行更改,并且VACCUUM已满,您可以执行以下操作:

select step_id, command_id, rank - 1 as command_order 
from (select step_id, command_id, ctid as wfc_ctid, rank() over
         (partition by step_id order by ctid)
      from wfc) as wfc_ordered;

这将为您提供所需的wfc表和订单。如果您确实更新了原始表,CTID将更改,因此使用上述查询创建表的副本可能更安全。

没有编辑问题的选项,只需添加wfc没有主键的表即可。我知道ctid可能很有用,但不确定如何在UpdateEyes中使用它,如果您是基于step_id和command_id的组合进行更新的,并且两行在这些字段中具有相同的值,那么它们将以相同的command_顺序进行更新。如果行没有唯一标识符,现在就有办法区分它们。@JuliaLeder我正在从另一个表更新,但是的,问题是步骤id和命令id的组合相同。那么是否可以按命令顺序使用不同的值进行更新?ctid在这里有什么帮助?按创建行的顺序对行进行编号对我不好。我需要从另一个表(wfp)进行更新。你知道我怎样才能做到吗?提前感谢如果您没有基于ctid进行更新,并且只查看步骤id和命令id,那么您将获得相同的命令顺序,因为绝对无法区分具有相同值的行。例如,您有两行,其中step_id=1和command_id=1。您会根据什么决定一行的命令顺序为0,另一行的命令顺序为4?你要么错过了一些信息,要么试图做一些不可能的事情。