MySQL 데이터베이스에서 문자열의 일부를 변경하도록 컬럼 값 업데이트하는 방법

워드프레스 정보를 제공하는 블로그 Avada 2018. 3. 16. 20:45 • 댓글:

예를 들어, 특정 테이블 내의 url 값이 http://domain1.com/images/img1.jpg인 것을 http://domain2.com/otherfolder/img1.jpg처럼 변경하고 싶은 경우가 있을 수 있습니다.

이 경우에 다음과 같은 MySQL 쿼리를 사용할 수 있습니다.

UPDATE yourtable
SET url = REPLACE(url, 'http://domain1.com/images/', 'http://domain2.com/otherfolder/')
WHERE url LIKE ('http://domain1.com/images/%');

관련 문서: https://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_replace

참고 문서: Update a column value, replacing part of a string

SQL Update 구문은 다음과 같은 형식으로 이용할 수 있습니다.

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

(SQL UPDATE Statement 참고)

SQL Replace() 함수는 다음 구문 형식으로 이용이 가능합니다.

REPLACE(string, from_substring, to_substring)

(MySQL REPLACE() Function 참고)