Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Concat unique或删除重复字符串php/mysql_Php_Mysql_Duplicate Removal_Concat - Fatal编程技术网

Concat unique或删除重复字符串php/mysql

Concat unique或删除重复字符串php/mysql,php,mysql,duplicate-removal,concat,Php,Mysql,Duplicate Removal,Concat,我需要删除表行中的重复项。 要组合这3个数字并生成所有可能的组合,请按.html表单发布 如果所有数字都不同,则为6。每个数字必须有6个数字,如: 123、234等。。。。如果一个数字与另一个数字相同,则减少组合的数量, 比如112。 我所做的,直到现在。。。 要隔离数字并将其存储在一行中,请执行以下操作: $cc=GetRow("SELECT numbers FROM table"); $n1=substr($cc, 0, 1); $n2=sub

我需要删除表行中的重复项。 要组合这3个数字并生成所有可能的组合,请按.html表单发布 如果所有数字都不同,则为6。每个数字必须有6个数字,如: 123、234等。。。。如果一个数字与另一个数字相同,则减少组合的数量, 比如112。 我所做的,直到现在。。。 要隔离数字并将其存储在一行中,请执行以下操作:

        $cc=GetRow("SELECT numbers FROM table");
        $n1=substr($cc, 0, 1);
        $n2=substr($cc, 1, 1);
        $n3=substr($cc, 2, 1);

        //scrambling the numbers
        $n1n2n3=$n1.$n2.$n3; //123 number stored
        $n1n3n2=$n1.$n3.$n2; //132 number stored
        $n2n1n3=$n2.$n1.$n3; //213 number stored
        $n2n3n1=$n2.$n3.$n1; //231 number stored
        $n3n1n2=$n3.$n1.$n2; //312 number stored
        $n3n2n1=$n3.$n2.$n1; //321 number stored

        $sql = sqlQuery("UPDATE table SET cc_concat = CONCAT_WS(',', '$n1n2n3', '$n1n3n2','$n2n1n3','$n2n3n1','$n3n1n2','$n3n2n1')");

        But here´s the problem:
        if the number is 112 will generate duplicates, only 3 are uniques:
        $n1n2n3=$n1.$n2.$n3; //112 number stored
        $n1n3n2=$n1.$n3.$n2; //121 number stored
        $n2n1n3=$n2.$n1.$n3; //112 number stored
        $n2n3n1=$n2.$n3.$n1; //121 number stored
        $n3n1n2=$n3.$n1.$n2; //211 number stored
        $n3n2n1=$n3.$n2.$n1; //211 number stored

        Is there a way to update the table without the duplicates? or remove the duplicates
        after update?

谢谢

您可以使用数组存储组合,并利用in_数组功能确保不会有重复的组合:

$combinations = array();
if ( !in_array( $n1n2n3, $combinations ) ) $combinations[] = $n1n2n3;
if ( !in_array( $n1n3n2, $combinations ) ) $combinations[] = $n1n3n2;
// Do the same for the rest

// Then you could easily concatenate the result set from PHP.
$cc_concat = implode( ',', $combinations );

// Finally update the database.
$sql = sqlQuery("UPDATE table SET cc_concat = '{$cc_concat}' ");    
这样的事情应该能解决问题

此外,您可能更喜欢使用算法从三位数的数组中生成组合。对于这种算法,您可以检查以下线程:

谢谢!工作起来很有魅力!我现在来看看算法。亚历杭德罗!不客气,我很高兴!。我真的很感激+1;当做我不被允许。。。我的名声很差:哦,没问题!尽管如此,我还是很乐意提供帮助: