正如已经提到的,正确的解决方案是规范化数据。但是,如果您被这个表结构卡住了,您仍然可以使用 3 个条件执行单个UPDATE
语句CASE
来匹配开头、中间或结尾的数字。
要将其从列表的开头或结尾删除,您可以使用 a SUBSTR()
,并继续使用REPLACE()
将其从列表的中间删除。
UPDATE
table
SET images =
CASE
/* if 3 is in the leftmost position in the list, remove first 2 chars... */
WHEN LEFT(images, 2) = '3,' THEN SUBSTR(images, 3)
/* if 3 is in the rightmost position in the list, remove last 2 chars... */
WHEN RIGHT(images, 2) = ',3' THEN SUBSTR(images, 1, LENGTH(images) - 2)
/* Otherwise replace a single 3 bounded by commas with a single comma */
ELSE REPLACE(images, ',3,', ',')
END
WHERE id = '1'