如何在mysql表中添加新条目,但仅保留1列,同时保留所有其他列数据

时间:2020-02-01 12:14:05

标签: mysql

所以我有下表

table1

但是当我运行以下命令时:

"INSERT INTO default_db.loader_messages (confirmation_secure_radio_button_message) VALUES ('$confirmmessagevalue1')";

它会添加一个新行,但会清空上面的loader_message1列。

请告诉我如何将当前数据保存在loader_message1列中以及添加新行?

2 个答案:

答案 0 :(得分:0)

如果您可以从insert..values更改为insert..select,则可以

drop table if exists t;
create table t (id int auto_increment primary key,col1 varchar(10),col2 varchar(10));
insert into t (col1) values ('col1');
insert into t (col1,col2) 
    select col1,'aaa'
    from t where id = (select max(id) from t)
;
insert into t (col1) values ('xxx');
insert into t (col1,col2) 
    select col1,'bbb'
    from t where id = (select max(id) from t)
;

select * from t;

+----+------+------+
| id | col1 | col2 |
+----+------+------+
|  1 | col1 | NULL |
|  2 | col1 | aaa  |
|  3 | xxx  | NULL |
|  4 | xxx  | bbb  |
+----+------+------+
4 rows in set (0.02 sec)

如果您无法做到这一点,那么请考虑在插入或插入过程之前在前端进行捕获,并在插入后使用更新进行填充。

答案 1 :(得分:0)

我提到将列值复制到多行是一个糟糕的设计。

还是...你应该

select loader_message1 from dbname where yourcondition;

将mysqli结果放入$ row,然后使用

$row['loader_message1'] or $row[0]

insert... (fld1, fld2) values (value1, value2)
相关问题