MYSQL:在预定事件中更新UPDATE后删除一行

时间:2014-06-10 04:26:15

标签: mysql sql

您好我有一个事件从较大的表中选择此结果 查询是:

UPDATE character_data tu, character_data ts 
SET tu.killsZ = ts.killsZ 
WHERE tu.playerUID = ts.playerUID 
    and tu.numberid < ts.numberid;

numberid  |  playerUID  |  alive  |  killsZ
   1           555555        0         55
   2           555555        1         55

事件就是

1. UPDATE character_data tu, character_data ts 
   SET tu.killsZ = ts.killsZ 
   WHERE tu.playerUID = ts.playerUID 
       and tu.numberid < ts.numberid;
2. DELETE FROM character_data 
   WHERE alive = 0 
   GROUP BY playerUID 
   HAVING COUNT(playerUID) > 1;

此行似乎无法正常工作:

DELETE FROM character_data 
WHERE alive = 0 
GROUP BY playerUID 
HAVING COUNT(playerUID) > 1;

奇怪的是,它会删除alive = 1而不是0的所有行。

如果我这样做:

DELETE FROM character_data 
WHERE alive = 1 
GROUP BY playerUID 
HAVING COUNT(playerUID) > 1;

它将删除alive = 0的所有行,但不时地删除右行。为什么这样做是相反的,我怎样才能获得可靠的结果呢?

3 个答案:

答案 0 :(得分:0)

这有效:

    delete from character_data  where playeruid in
(select t1.playeruid from character_data1 t1 where playeruid=t1.playeruid 
and killsz=t1.killsz and alive=0 
group by playeruid having count(*)>1) limit 1

注意:

仅当有2个重复记录时才有效。如果你有三个重复的playerid并想要删除其中的2个,那么你应该手动将限制改为2.表table_data1是我创建的临时表你在使用删除功能时,无法在子查询中使用 相同的表格,因此您应该创建一个重复的表来执行任务。

答案 1 :(得分:0)

如果我正确读取,你想要做的是删除alive = 0的行,但仅限于那些playerUID值出现多次的行。所以你要找的是一个至少有一个副本的playerUID值列表:

select playerUID
from character_data
group by playerUID
having count(*) > 1

然后使用该列表删除alive = 0:

的位置
delete from character_data
where alive = 0
and playerUID in( query-shown-above );

答案 2 :(得分:-1)

我建议删除以下查询:

DELETE FROM character_data 
WHERE alive = 0 
and playerUID is not null

我不知道该小组是什么让你真正得到了什么