我需要从表格行中删除重复项。 合并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?
谢谢!
答案 0 :(得分:0)
您可以使用数组来存储组合并利用in_array()函数来确保您不会有重复项:
$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}' ");
这样的事情应该可以解决问题。
此外,您可能更喜欢使用算法从三位数组中为您生成组合。对于这样的算法,你可以检查这个线程: Get all permutations of a PHP array?