从 SET 字段中删除值的最佳方法?

编程入门 行业动态 更新时间:2024-10-23 21:36:09
本文介绍了从 SET 字段中删除值的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

更新 mysql SET 字段的最佳方法是从字段中删除特定值.

Which is the best way to update a mysql SET field, to remove a specific value from the field.

例如.具有值的字段类别:1、2、3、4、5?我想从列表中删除2":

Eg. field categories with values: 1,2,3,4,5? I want to remove '2' from the list:

UPDATE table SET categories = REPLACE(categories, ',2,', ',') WHERE field LIKE '%,2,%';

但是如果 '2' 是列表中的第一个或最后一个值呢?

But what if '2' is the first or the last value from the list?

UPDATE table SET categories = REPLACE(categories, '2,', '') WHERE field LIKE '2,%'; UPDATE table SET categories = REPLACE(categories, ',2', '') WHERE field LIKE ',2%';

如何用一个查询处理所有 3 个案例?!

How could I handle all 3 cases with one single query?!

推荐答案

如果你需要从集合中移除的值不能多次出现,你可以使用这个:

If the value you need to remove from the set can't be present more than once, you could use this:

UPDATE yourtable SET categories = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', categories, ','), ',2,', ',')) WHERE FIND_IN_SET('2', categories)

看到它在这里工作.如果该值可以多次出现,这将删除它的所有出现:

see it working here. If the value can be present more than once, this will remove all occourences of it:

UPDATE yourtable SET categories = TRIM(BOTH ',' FROM REPLACE( REPLACE(CONCAT(',',REPLACE(col, ',', ',,'), ','),',2,', ''), ',,', ',') ) WHERE FIND_IN_SET('2', categories)

更多推荐

从 SET 字段中删除值的最佳方法?

本文发布于:2023-11-01 18:28:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550081.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   方法   SET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!