user3961151
user3961151

Reputation:

SELECT and then update in the same column

I have this in my DB:

| id           | time        | time-left   |
| 1            | 242363246   | 1           |
| 2            | 215625252   | 1           |
| 3            | 147852369   | 1           |
| 4            | 951753456   | 1           |

How can I insert into time-left the same as in time but with 111 more? Here is how I want it to become:

| id           | time        | time-left   |
| 1            | 242363246   | 242363357   |
| 2            | 215625252   | 215625363   |
| 3            | 147852369   | 147852480   |
| 4            | 951753456   | 951753567   |

Upvotes: 0

Views: 34

Answers (3)

jpw
jpw

Reputation: 44881

Assuming time is an integer or a char type that can be incremented (with implicit conversion):

UPDATE your_table
SET "time-left" = time + 111;

If timecan't be incremented this way you have to cast/convert it to an integer type first.

Depending on what DBMS you are using the way to use quoted identifiers for invalid names (like time-leftin this case) might be something else like using brackets: [time-left] (SQL Server) or using backticks: `time-left` (MySQL).

Upvotes: 2

Dave Zych
Dave Zych

Reputation: 21887

update MyTable
set time-left = time + 111

Upvotes: 0

amphetamachine
amphetamachine

Reputation: 30595

update myTable set "time-left" = time + 111;

Upvotes: 1

Related Questions