I have a mapping table with the only difference from the tuple (c_id, t_id)
.
Here are some sample data to illustrate the situation:
id c_id t_id ---------------- 1 10 2 2 10 3 3 10 7 4 12 2 5 13 3
I wrote a merge function for t_ids
(x, y → z OR x, y → x). If my content ( c_id
) has t_ids
, then of course I violate the restriction using this statement:
UPDATE mapping_table SET t_id = '$target_tid' WHERE t_id = '$t1_id' OR t_id = '$t2_id';
Result:
id c_id t_id ---------------- 1 10 4 2 10 4 3 10 7
Now I came up with the following:
DELETE FROM mapping_table WHERE ( SELECT count(c_id) FROM mapping_table WHERE t_id = '$t1_id' OR t_id = '$t2_id' ) > 1; UPDATE mapping_table SET t_id = '$target_tid' WHERE t_id = '$t1_id' OR t_id = '$t2_id';
Now I get the following error:
You can't specify target table 'mapping_table' for update in FROM clause
My questions:
- What exactly is wrong here? Is the
DELETE
seen as an update and cannot be used in a WHERE
? - Is this a more efficient way to do this?
source share