如何在SQL中更新以获取不同的元组/不违反唯一约束

时间:2013-02-23 17:20:52

标签: mysql merge sql-update duplicates unique-constraint

我有一个映射表,在元组(c_id, t_id)上有唯一的约束。

以下是一些示例数据来说明情况:

id  c_id    t_id  
----------------
1   10      2
2   10      3
3   10      7
4   12      2
5   13      3

我为t_ids写了一个合并函数(x,y - > z OR x,y - > x)。 如果我的内容(c_id)同时包含t_ids,那么我当然会通过使用此声明来违反约束:

UPDATE mapping_table
SET t_id = '$target_tid'
WHERE t_id = '$t1_id' OR t_id = '$t2_id';

结果将是:

id  c_id    t_id
----------------
1   10      4
2   10      4       /* violates unique constraint */
3   10      7

现在我想出了这个:

/* delete one of the duplicate entries */
DELETE FROM mapping_table
WHERE   ( SELECT count(c_id)
          FROM mapping_table
          WHERE t_id = '$t1_id' OR t_id = '$t2_id'
        ) > 1;

/* update the remaining row */
UPDATE mapping_table
SET t_id = '$target_tid'
WHERE t_id = '$t1_id' OR t_id = '$t2_id';

现在我收到以下错误:
You can't specify target table 'mapping_table' for update in FROM clause

我的问题是:

  1. 这里到底出了什么问题? DELETE语句是否被视为更新,不能在WHERE子句中使用?
  2. 这有更有效的方法吗?

3 个答案:

答案 0 :(得分:1)

您遇到的错误是MySQL的特点。你可以通过一组双子查询来解决这个问题:

DELETE FROM mapping_table
WHERE  (select *
        from ( SELECT count(c_id)
               FROM mapping_table
               WHERE t_id = '$t1_id' OR t_id = '$t2_id'
             ) > 1
        ) t

要解决您的问题,请删除除最小值之外的所有ID。我认为这也可行:

delete from mapping_table
where id > (select minid from (select min(id) from mapping_table mt2
                               where mt2.c_id = mapping_table.c_id and
                                     mt2.t_id = mapping_table.t_id
                              )
           )

您还可以将ID列表存储在临时表中,并在查询中使用它:

create temporary table minids as
     select c_id, t_id, min(id) as minid
     from mapping_table
     group by c_id, t_id;

delete from mapping_table
where exists (select 1 from minids
              where mt2.c_id = mapping_table.c_id and
                    mt2.t_id = mapping_table.t_id and
                    mt2.minid > mapping_table.id
             )

答案 1 :(得分:0)

试试这个

DELETE FROM mapping_table
    WHERE   ( SELECT count(c_id)
      FROM mapping_table
      WHERE t_id = '$t1_id' OR t_id = '$t2_id'
      Having count(c_id) > 1
    );

编辑:在您的更新声明中尝试此操作

 UPDATE mapping_table
 SET t_id = '$target_tid'
WHERE t_id in (select t_id from mapping_table where t_id= '$t1_id' OR t_id = '$t2_id') 

答案 2 :(得分:0)

我一直在寻找这个解决方案。性能可能非常低,但至少我找到了一个有效的解决方案(并学到了一些东西)。

/* actually delete rows that will become duplicate after the update */
DELETE FROM mt1 USING mapping_table AS mt1 WHERE id IN (

    /* sub-query to allow `mapping_table` in the DELETE statement */
    SELECT * FROM (

        /* select ids/rows with one t_id available */
        SELECT id
        FROM mapping_table AS mt2
        WHERE mt2.tag_id = $t1_id AND c_id IN (

            /* select ids/rows with both t_id available */
            SELECT c_id
            FROM mapping_table AS mt3
            WHERE mt3.c_id = mt2.c_id AND mt3.tag_id = $t2_id)

    /* alias needed for every derived table */
    ) as mres
)

/* Update to merge t_ids */
UPDATE mapping_table
SET t_id = '$target_id'
WHERE t_id = '$t1_id' OR t_id = '$t2_id';