ayasha
ayasha

Reputation: 1251

Update a Column from another Column using SQLite?

I have a DB with two columns with many records and from these two columns I have to do some mathematical operations and create other two columns.

For now I did like this:

Do you think there is a faster way to do it? I also tried like this:

UPDATE myTable SET X_GAUSS = (SELECT X FROM myTable ) + 1, Y_GAUSS = (SELECT Y FROM myTable) + 2

(it's only an example)

But in this way every line of the new columns is the same as the line before, instead I want something like:

X  Y  X_GAUSS Y_GAUSS
1  2  2       4
3  4  4       6
5  6  6       8
...  

Upvotes: 9

Views: 6010

Answers (1)

CL.
CL.

Reputation: 180060

A subquery like SELECT X FROM myTable returns the first row of the table.

You can simply access the columns of the same row directly:

UPDATE myTable
SET X_GAUSS = X + 1,
    Y_GAUSS = Y + 2;

Upvotes: 10

Related Questions