2014-04-08 111 views
0
create table myTable (int i, int user_id, int a, int b, int c) 

这些值是这样的。如何根据existin列计算列值

1, 1, 1, 1, 0 
2, 1, 2, 2, 0 
3, 2, 3, 3, 0 
4, 2, 4, 4, 0 

我想将列“c”更新为user_id = 1的“a”+“b”。我应该怎么做?

回答

1

Select查询

SELECT a, b, (a+b) AS c FROM mytable where user_id=1; 

UPDATE查询

UPDATE mytable SET c = a+b where user_id=1; 
3

试试这个,

update myTable set c = a+b where user_id =1; 

Demo

+1

@M哈立德·朱奈德,谢谢! – AK47

1

对于相提并论满足特殊记录:

update some_table set `c` = (`a`+`b`) where `user_id` = your_id; 

对于所有:

update some_table set `c` = (`a`+`b`);