drop table if exists t;
create table t(ID int, NAME varchar(20), POSition varchar(20),NOTES varchar(500));
insert into t values
(1 , 'Richard' , 'Programmer' , 'Not paid enough'),
(2 , 'David' , 'Manager' , 'Lazy'),
(3 , 'Hilary' , 'Personnel Manager','Doesnt care');
update t
set notes =
CONCAT(NOTES, ' - ', NAME, ' - ', POSITION)
where 1 = 1;
select * from t;
+------+---------+-------------------+------------------------------------------+
| ID | NAME | POSition | NOTES |
+------+---------+-------------------+------------------------------------------+
| 1 | Richard | Programmer | Not paid enough - Richard - Programmer |
| 2 | David | Manager | Lazy - David - Manager |
| 3 | Hilary | Personnel Manager | Doesnt care - Hilary - Personnel Manager |
+------+---------+-------------------+------------------------------------------+
3 rows in set (0.00 sec)
В конечном счете, я хочу удалить поля ИМЯ и ПОЛОЖЕНИЕ Вы уверены, что хотите это сделать? выпустить кусочки снова - это боль.
Если какой-либо из элементов имеет значение null, concat вернет значение null, если элемент не имеет значения, тогда необходим тест ifnull
update t
set notes =
CONCAT(IFNULL(NOTES,''), ' - ', IFNULL(NAME,''), ' - ', IFNULL(POSITION,''))
where 1 = 1;
drop table if exists t;
create table t(ID int, NAME varchar(20), POSition varchar(20),NOTES varchar(500));
insert into t values
(1 , 'Richard' , 'Programmer' , 'Not paid enough'),
(2 , 'David' , 'Manager' , 'Lazy'),
(3 , 'Hilary' , 'Personnel Manager','Doesnt care'),
(4 , 'HILARY' , NULL,'DOES NOW') ;
update t
set notes =
CONCAT(IFNULL(NOTES,''), ' - ', IFNULL(NAME,''), ' - ', IFNULL(POSITION,''))
where 1 = 1;
+------+---------+-------------------+------------------------------------------+
| ID | NAME | POSition | NOTES |
+------+---------+-------------------+------------------------------------------+
| 1 | Richard | Programmer | Not paid enough - Richard - Programmer |
| 2 | David | Manager | Lazy - David - Manager |
| 3 | Hilary | Personnel Manager | Doesnt care - Hilary - Personnel Manager |
| 4 | HILARY | NULL | DOES NOW - HILARY - |
+------+---------+-------------------+------------------------------------------+
4 rows in set (0.00 sec)