Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.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

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
Sql 从具有不同关键字的同一列复制列中的值_Sql_Postgresql - Fatal编程技术网

Sql 从具有不同关键字的同一列复制列中的值

Sql 从具有不同关键字的同一列复制列中的值,sql,postgresql,Sql,Postgresql,我在博士后有下表 id | col1 |col2 |col3 |col4 66 | AfBLy_2_d1_1 | Sample |6798 | Day 1 67 | AfBLy_2_d1_6 | Sample |8798 | Day 2 66 | AfBLy_2_d1_4 | Sample |6776 | Day 7 69 | AfBLy_2_d1_9 | Sample |6789 | Day 5 66 | AfBLy_2_d1_1 | Sampl

我在博士后有下表

id | col1         |col2      |col3  |col4
66 | AfBLy_2_d1_1 | Sample |6798  | Day 1  
67 | AfBLy_2_d1_6 | Sample |8798 | Day 2  
66 | AfBLy_2_d1_4 | Sample |6776 | Day 7  
69 | AfBLy_2_d1_9 | Sample |6789 | Day 5  

66 | AfBLy_2_d1_1 | Sample Type  |  | Day 1
69 | AfBLy_2_d1_6 | Sample Type |  | Day 2  
66 | AfBLy_2_d1_4 | Sample Type |  | Day 7  
67 | AfBLy_2_d1_9 | Sample Type |   | Day 5  

如何将第3列col2=Sample的值复制到第3列col2=Sample Type对应的col1和id值

with cte as (
    select * from your_table
    where col2 = 'Sample'
)
update your_table as t1
set t1.col3 = t2.col3
from cte as t2
where t1.col2 = 'Sample Type'
and t1.col1 = t2.col1
and t1.id = t2.id;

您可以如下方式更新表:

with cte as (
    select * from your_table
    where col2 = 'Sample'
)
update your_table as t1
set t1.col3 = t2.col3
from cte as t2
where t1.col2 = 'Sample Type'
and t1.col1 = t2.col1
and t1.id = t2.id;