如何在与其他行重复时删除mysql中的行?

时间:2014-03-12 11:54:17

标签: php mysql sql

如何在与其他行重复时删除mysql中的行?

我想创建页面delete_duplicate.php

当我加载pagr delete_duplicate.php

它将删除行ID 3和4

因为行id 3重复行id 2和行id 4重复行id 1

如何编写代码[php]?

在加载页面之前delete_duplicate.php ------ table:member

 ____________________________________
|__likeid__|__userid__|__product_id__|
|____1_____|__100261__|____aaaaaa____|
|____2_____|__100005__|____bbbbbb____|
|____3_____|__100005__|____bbbbbb____|
|____4_____|__100261__|____aaaaaa____|
|____5_____|__100261__|____cccccc____|

加载页面delete_duplicate.php ------ table:member

 ____________________________________
|__likeid__|__userid__|__product_id__|
|____1_____|__100261__|____aaaaaa____|
|____2_____|__100005__|____bbbbbb____|
|____5_____|__100261__|____cccccc____|

6 个答案:

答案 0 :(得分:3)

您可以通过在user_idproduct_id上创建唯一索引来永久解决此问题:

create unique index member_userid_productid on member(userid, productid);

对于您的示例,您可以delete使用join删除重复项:

delete m from members m join
              (select m.user_id, m.product_id, min(m.like_id) as minlike_id
               from members m
               group by m.user_id, m.product_id
              ) tokeep
              on m.user_id = tokeep.user_id and
                 m.product_id = tokeep.product_id and
                 m.like_id > tokeep.minlike_id;

答案 1 :(得分:2)

您应发出以下声明,以便从表中删除重复记录并实现product_id的唯一性:

DELETE e1 FROM member e1, member e2 WHERE e1.product_id = e2.product_id AND e1.likeid > e2.likeid;

答案 2 :(得分:1)

如果你想通过SQL实现它,那么下面的查询可以帮助

DELETE FROM member m1, member m2 WHERE m1.likeid > m2.likeid 
AND m1.userid = m2.userid 
AND m1.product_id = m2.product_id

答案 3 :(得分:1)

如果你知道如何从php执行它,你可以删除这个mysql查询的所有重复项。

DELETE FROM `member`
WHERE `member`.`likeid` NOT IN (
    SELECT `temp_member`.`min_likeid`
    FROM (
        SELECT MIN(`likeid`) AS `min_likeid`
        FROM `member`
        GROUP BY `member`.`userid`, `member`.`product_id`


    ) AS `temp_member`)

答案 4 :(得分:0)

您可以在创建数据库条目之前进行检查(最佳实践):

  • 对组合userid / product_id进行唯一约束。

您可以在数据库中输入重复的条目后将其删除:

  • 使用SQL命令SELECT DISTINCT
  • 将表格加载到php代码中
  • 清空你的桌子
  • 将php条目写回您的表格。

答案 5 :(得分:0)

  

编辑:仅当您的主键为" id",将ID重叠为" likeid"

时,此功能才有效

你可以用php

来做

类似

$config['db'] = array(
    'host'          => 'localhost',
    'username'      => 'root',
    'password'      => 'root',
    'dbname'        => 'dbname'
    );

$db = new PDO("mysql:host={$config['db']['host']};dbname={$config['db']['dbname']}",
              $config['db']['username'], $config['db']['password']);

$query = "SELECT * FROM member";

$stmt = $db->prepare($query);
$members = $stmt->fetch(PDO::FETCH_ASSOC);

现在你拥有$ members中的所有成员,所以你可以循环遍历它们

$idsToDelete= [];
$array = [];

foreach($members as $member)
{

    if(in_array($member['product_id'], $array)){
        $idsToDelete[] = $member['id'];
    }

    $array[] = $member['product_id'];
}

删除重复项

$removeQuery  = "DELETE FROM from members WHERE id in (:idsToDelete)";
$stmt = $db->prepare($removeQuery);
$stmt->bindParam(':idsToDelete', $idsToDelete);
$stmt->execute();