Mysql:用正则表达式替换迭代元素


mysql: replacing iterating elements with regular expressions

我试图更新一个表,其中一个名为"operatori"的字段包含一组数字。这是一个例子。2、4、4、4、4、4

我想做的是去掉集合中同一元素的多次迭代,得到一个更简洁的,比如:2,4。

我已经尝试用4替换4,4,4,4,4,但只有当我有5个相同元素的迭代并且只有数字4时才有效。

数字从0到7,迭代可以从1到n

我认为正则表达式可以做到这一点,但是我真的很难理解它们。

你有什么提示吗?

try this,

$str = "2,4,4,4,4,4";
$array = explode(",",$str);
$array = array_unique($array);
print_r($array);

输出将是

Array
(
    [0] => 2
    [1] => 4
)

您可能使用了错误的数据结构。通常,您想要一个每项一行的连接表,而不是将项存储在列表中。

但是,如果只有8个项目,您可以执行以下操作——如果您不关心顺序。

select concat_ws(',',
                 (case when find_in_set('0', operatori) > 0 then '0' end),
                 (case when find_in_set('1', operatori) > 0 then '1' end),
                 (case when find_in_set('2', operatori) > 0 then '2' end),
                 (case when find_in_set('3', operatori) > 0 then '3' end),
                 (case when find_in_set('4', operatori) > 0 then '4' end),
                 (case when find_in_set('5', operatori) > 0 then '5' end),
                 (case when find_in_set('6', operatori) > 0 then '6' end),
                 (case when find_in_set('7', operatori) > 0 then '7' end)
                )
. . .

此逻辑也可用于update语句。