使用PHP从MySQL列中删除重复的逗号分隔值

时间:2015-12-31 21:37:34

标签: php mysql

我有一个包含以下列的数据库:

id     options
1      Website,Website,Newspaper,Newspaper,TV,TV,Radio,Radio
2      Website,Website,Newspaper,Newspaper
3      Website,Website,TV,TV

目标是删除重复的条目,并将options列标准化为:

id     options
1      Website,Newspaper,TV,Radio
2      Website,Newspaper
3      Website,TV

我开发了以下PHP代码:

$sql = "SELECT id, options FROM table";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $id = $row['id'];
        $values_array = explode( ',' , $row['options'] );
        if(count($values_array) != count(array_unique($values_array)))
        {
            $likes = array_unique($values_array);
            $new = implode(',', $likes);
            $sql = "UPDATE table SET options=".$new." WHERE id = '$id'";
        }
    }
} else {
    echo "0 results";
}
$conn->close();

这不能完成工作。一切似乎都有效,但尝试使用新的数组数据更新options列。

这似乎并不太困难,只是寻找一些如何使其发挥作用的指导。

提前致谢!

5 个答案:

答案 0 :(得分:2)

你可以直接在mysql

中完成
UPDATE T
 JOIN
 (SELECT id,GROUP_CONCAT(DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(t.options, ',', sub0.aNum), ',', -1)) AS ids
FROM t
INNER JOIN
(
    SELECT 1 + units.i + tens.i * 10 AS aNum, units.i + tens.i * 10 AS aSubscript
    FROM (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) units
    CROSS JOIN (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) tens
) sub0
ON (1 + LENGTH(t.options) - LENGTH(REPLACE(t.options, ',', ''))) >= sub0.aNum
GROUP BY id)x
ON x.id=t.id
SET t.options=x.ids

FIDDLE

受此answer启发

答案 1 :(得分:1)

试试这个:

$sql = "SELECT id, options FROM table";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
    while ($row = $result->fetch_assoc()) {
        $id = $row['id'];
        $values_array = explode(',', $row['options']);
        if (count($values_array) != count(array_unique($values_array))) {
            $likes = array_unique($values_array);
            $new = implode(',', $likes);
            $sql = "UPDATE table SET options=" . $new . " WHERE id = '$id'";
            /* seems you missed this */
            if ($conn->query($sql) === TRUE) {
                echo "Record updated successfully";
            } else {
                echo "Error updating record: " . $conn->error;
            }
            /* you declared sql query but not executed it */
        }
    }
} else {
    echo "0 results";
}
$conn->close();

希望它有用:)

答案 2 :(得分:1)

New必须在引号中:

$sql = "UPDATE table SET options='".$new."' WHERE id='$id'";


之前的输出:

  

更新表SET选项= 新值 WHERE ...

Ouptut之后:

  

更新表SET选项= '新值 WHERE ...

答案 3 :(得分:1)

正如其他答案中所述,您的引言有误并且您没有执行UPDATE查询,还有另外您需要知道的事情。

table是MySQL中的保留关键字,因此您无法在查询中使用它。使用反引号逃脱它。

所以你的代码应该是这样的:

$sql = "SELECT id, options FROM `table`";
$result = $conn->query($sql);

if ($result->num_rows > 0){
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $id = $row['id'];
        $values_array = explode( ',' , $row['options'] );
        if(count($values_array) != count(array_unique($values_array))){
            $likes = array_unique($values_array);
            $new = implode(',', $likes);
            $sql = "UPDATE `table` SET options='".$new."' WHERE id = '$id'";
            $conn->query($sql);
            if($conn->affected_rows){
                echo "success<br />";
            }else{
                echo "error<br />";
            }
        }
    }
}else{
    echo "0 results";
}
$conn->close();

以下是参考资料:

答案 4 :(得分:1)

我将以下代码添加到我的PHP中,由Ajjay Aroraa展示 -

if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}

我整个申请的最终代码:

$sql = "SELECT id, options FROM tdata";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$id = $row['id'];
$values_array = explode( ',' , $row['options'] );
if(count($values_array) != count(array_unique($values_array)))
{
// find duplicate values in the array
$likes = array_unique($values_array);
$new = implode(',', $likes);
$sql = "UPDATE tdata SET options='".$new."' WHERE id = '$id'";
// execute update query
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
}
}
} else {
echo "0 results";
}
$conn->close();

感谢所有回复的人 - 这是一个快速修复,因为我正在努力规范化表格。