使用其他列值更新列

时间:2016-06-14 18:03:06

标签: mysql

给出下表

id   name             Date
1    Hello_world_@13  15/05/2016
2    Bye_world_@22    16/07/2016
3    Random_Name_@75  18/09/2016

我需要更新名称列,将日期值插入其中。我需要在“@”之前插入日期,并在日期末尾添加“_”。

id   name                        Date
1    Hello_world_15/05/2016_@13  15/05/2016
2    Bye_world_16/07/2016_@22    16/07/2016
3    Random_Name_18/09/2016_@75  18/09/2016

有关查询的任何想法会这样做吗?

2 个答案:

答案 0 :(得分:1)

使用替换

如果日期列已经是字符串,则使用

update table 
set name = replace(name, '@' , concat(date, '_@'));

否则也使用date_format

update table 
set name = replace(name, '@' , concat(DATE_FORMAT(date, '%d/%m/%Y'), '_@'));

答案 1 :(得分:1)

您可以使用REPLACE a CONCAT:

来完成 在你的桌子上

使用这个你必须连接日期和@

UPDATE yourTable SET NAME = REPLACE(`NAME`, '@' , CONCAT(`DATE`,'_@'));

<强>样品

MariaDB []> SELECT REPLACE('Hello_world_@13', '@' , '15/05/2016_@');
+-------------------------------------------------+
| REPLACE('Hello_world_@13', '@' , '15/05/2016_@') |
+-------------------------------------------------+
| Hello_world_15/05/2016_@13                       |
+-------------------------------------------------+
1 row in set (0.00 sec)

MariaDB []>