반응형
MySQL UPDATE 데이터를 열에 추가
테이블 이름을 업데이트해야 합니다(col1 name).
이미 데이터가 있으면 'a,b,c' 값을 추가해야 합니다. NULL이면 'a,b,c' 값을 추가해야 합니다.
CONCAT 논법이 있다는 것은 알고 있지만 SQL 구문이 무엇인지는 확실하지 않습니다.
update tablename set col1name = concat(ifnull(col1name, 'a,b,c'), 'a,b,c')
위의 내용이 맞습니까?
다음 쿼리 시도:
update tablename set col1name = concat(ifnull(col1name,""), 'a,b,c');
이렇게 하면 됩니다.
update tablename set
col1name = if(col1name is null, 'a,b,c', concat(col1name, 'a,b,c'));
또는 두 단계로 수행함으로써 삶을 더 쉽게 만들 수 있습니다.
update tablename set col1name = '' where col1name is null;
그리고나서
update tablename set col1name = concat(col1name, 'a,b,c');
다음을 사용할 수 있습니다.
update yourtable
set yourcol = case when yourcol is null then 'a,b,c'
else concat(yourcol, ' a,b,c') end
샘플 데이터:
CREATE TABLE yourtable(`yourcol` varchar(50));
INSERT INTO yourtable(`yourcol`)
VALUES ('sadsdh'),
(NULL);
반환 예정:
| YOURCOL |
----------------
| sadsdh a,b,c |
| a,b,c |
IFNULL(열,""), if 문을 저장하면 SQL이 훨씬 간단해집니다!
MySQL 5.6 스키마 설정:
CREATE TABLE tablename
(`yourcol` varchar(50))
;
INSERT INTO tablename
(`yourcol`)
VALUES
('sadsdh'),
(NULL)
;
UPDATE tablename SET
yourcol = CONCAT( IFNULL(yourcol,' '), 'somevalue' )
;
쿼리:
select *
from tablename
결과:
| yourcol |
|-----------------|
| sadsdhsomevalue |
| somevalue |
ifnull 함수를 쓰는 이유: col1name1이 비어 있으면 null+'a,b,c'에 연결된다는 것은 null+'a,b,c'를 의미하는 것이 분명합니다. 단순히 'a,b,c' set col1name = concat(ifnull(col1name)','a,b,c'). 이 대신 set col1name = concat(col1name, 'a,b,c')를 직접 쓸 수 있습니다.
언급URL : https://stackoverflow.com/questions/14020867/mysql-update-append-data-into-column
반응형
'IT' 카테고리의 다른 글
formControlName을 사용하고 중첩된 formGroup을 처리하는 방법은 무엇입니까? (0) | 2023.10.15 |
---|---|
엑셀병합셀일시 (0) | 2023.10.15 |
Spring Data @Query 주석 값 안에 상수를 사용하는 방법이 있습니까? (0) | 2023.10.15 |
누가 jQuery File Upload 플러그인 구현 방법을 설명해 줄 수 있습니까? (0) | 2023.10.15 |
IntelliJ에서 셀레늄 서버를 시작하려고 할 때 jar 파일이 잘못되었거나 손상되었습니다. (0) | 2023.10.15 |