如何使用相同的代码更新字段

时间:2017-10-27 10:04:46

标签: mysql

当PARAM有单词apple时,我想更新字段UPDATPARAM,只更新where子句= CODE的第一行

ID CODE PARAM UPDATPARAM
1  L12  MANGO  
2  L12  APPLE
3  L12  ORANGE
4  L13  APPLE  
5  L13  MANGO
6  L14  ORANGE
7  L14  MANGO
8  L14  Apple

期望的输出:

ID CODE PARAM UPDATPARAM
1  L12  MANGO    APPLE
2  L12  APPLE
3  L12  ORANGE  
4  L13  Apple   Apple 
5  L13  Orange
6  L14  ORANGE Apple
7  L14  MANGO
8  L14  Apple         

2 个答案:

答案 0 :(得分:2)

UPDATE your_table_name_here
SET UPDATPARAM = 'APPPLE'
WHERE PARAM LIKE 'APPLE';

可能这可以帮到你

答案 1 :(得分:0)

你可能想要这个

drop table if exists t;

create table t(ID int, CODE varchar(3), PARAM varchar(20), UPDATPARAM varchar(20));
insert into t(id,code,param) values
(1 , 'L12' , 'MANGO' ), 
(2 , 'L12' , 'APPLE'),
(3 , 'L12' , 'ORANGE');

update t as t1 join t t2 on t2.id = t1.id + 1
set t1.updatparam = t2.param
where t2.param = 'apple' and t1.id = 1;

select * from t;

+------+------+--------+------------+
| ID   | CODE | PARAM  | UPDATPARAM |
+------+------+--------+------------+
|    1 | L12  | MANGO  | APPLE      |
|    2 | L12  | APPLE  | NULL       |
|    3 | L12  | ORANGE | NULL       |
+------+------+--------+------------+
3 rows in set (0.00 sec)