简体   繁体   中英

Postgresql. How to copy the value of a cell in a row and paste it into another cell of another row in the same table using python

I'm starting studying Python and PostgreSQL.

I'm trying to update the value of a cell in a row, based on the value of another cell in another row of the same table.

Let's made an example: I've this table:

ID | name | age  
1  | John |  20
2  | Jack |  30
3  | Mary |  40

What I would like to do is to make a query that change the age of John based on the age of Jack (copy the age of Jack and paste it in the age of John)

and so this will be the result to be achieved:

ID | name | age  
1  | John |  30
2  | Jack |  30
3  | Mary |  40

Someone could help me? thanks

One way is to use a subquery in the assignment in the SET clause.

UPDATE elbat
       SET age = (SELECT t.age
                         FROM elbat t
                         WHERE t.name = 'Jack')
       WHERE name = 'John';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM