如何使用单个mysql查询批量或批量更新多个条件或条件的多个字段或列

时间:2016-05-10 14:12:48

标签: mysql sql-update case

这是我的表在查询之前的样子。

cid  | purl      | purl_full
----------------------------
2050 | dingo     | http://dingo.domain.com
2050 | wombat    | http://wombat.domain.com
2050 | platypus  | http://platypus.domain.com
2050 | koala     | http://koala.domain.com
2050 | dingo1    | http://dingo1.domain.com
2050 | wombat1   | http://wombat1.domain.com
2050 | platypus1 | http://platypus1.domain.com
2050 | koala1    | http://koala1.domain.com
2050 | dingo     | tempurl
2050 | wombat    | tempurl
2050 | platypus  | tempurl
2050 | koala     | tempurl

这是查询后的样子。

cid  | purl      | purl_full
----------------------------
2050 | dingo     | http://dingo.domain.com
2050 | wombat    | http://wombat.domain.com
2050 | platypus  | http://platypus.domain.com
2050 | koala     | http://koala.domain.com
2050 | dingo1    | http://dingo1.domain.com
2050 | wombat1   | http://wombat1.domain.com
2050 | platypus1 | http://platypus1.domain.com
2050 | koala1    | http://koala1.domain.com
2050 | dingo2    | http://dingo2.domain.com
2050 | wombat2   | http://wombat2.domain.com
2050 | platypus2 | http://platypus2.domain.com
2050 | koala2    | http://koala2.domain.com

这就是查询后的结果。

cid  | purl      | purl_full
----------------------------
2050 | dingo     | http://dingo.domain.com
2050 | wombat    | http://wombat.domain.com
2050 | platypus  | http://platypus.domain.com
2050 | koala     | http://koala.domain.com
2050 | dingo1    | http://dingo1.domain.com
2050 | wombat1   | http://wombat1.domain.com
2050 | platypus1 | http://platypus1.domain.com
2050 | koala1    | http://koala1.domain.com
2050 | dingo2    | 
2050 | wombat2   | 
2050 | platypus2 | 
2050 | koala2    | 

以下是我正在使用的查询。

UPDATE localhost_campaigns.list_data SET 
purl = (CASE 
WHEN (purl = 'dingo' AND purl_full = 'tempurl' AND cid = 2050) THEN 'dingo2' 
WHEN (purl = 'wombat' AND purl_full = 'tempurl' AND cid = 2050) THEN 'wombat2' 
WHEN (purl = 'platypus' AND purl_full = 'tempurl' AND cid = 2050) THEN 'platypus2' 
WHEN (purl = 'koala' AND purl_full = 'tempurl' AND cid = 2050) THEN 'koala2' 
END), 
purl_full = (CASE 
WHEN (purl = 'dingo' AND purl_full = 'tempurl' AND cid = 2050) THEN 'http://dingo2.domain.com' 
WHEN (purl = 'wombat' AND purl_full = 'tempurl' AND cid = 2050) THEN 'http://wombat2.domain.com' 
WHEN (purl = 'platypus' AND purl_full = 'tempurl' AND cid = 2050) THEN 'http://platypus2.domain.com' 
WHEN (purl = 'koala' AND purl_full = 'tempurl' AND cid = 2050) THEN 'http://koala2.domain.com' 
END) 
WHERE (purl IN ('dingo', 'wombat', 'platypus', 'koala') AND purl_full = 'tempurl' AND cid = 2050);

因此它更新了一个字段,但没有更新另一个字段。由于某种原因,它只是将第二个字段清空。

情况更糟:

UPDATE localhost_campaigns.list_data SET 
purl = CASE 
WHEN (purl = 'dingo' AND purl_full = 'tempurl' AND cid = 2050) THEN 'dingo2' 
WHEN (purl = 'wombat' AND purl_full = 'tempurl' AND cid = 2050) THEN 'wombat2' 
WHEN (purl = 'platypus' AND purl_full = 'tempurl' AND cid = 2050) THEN 'platypus2' 
WHEN (purl = 'koala' AND purl_full = 'tempurl' AND cid = 2050) THEN 'koala2' 
END, 
purl_full = CASE 
WHEN (purl = 'dingo' AND purl_full = 'tempurl' AND cid = 2050) THEN 'http://dingo2.domain.com' 
WHEN (purl = 'wombat' AND purl_full = 'tempurl' AND cid = 2050) THEN 'http://wombat2.domain.com' 
WHEN (purl = 'platypus' AND purl_full = 'tempurl' AND cid = 2050) THEN 'http://platypus2.domain.com' 
WHEN (purl = 'koala' AND purl_full = 'tempurl' AND cid = 2050) THEN 'http://koala2.domain.com' 
END; 

还尝试了这个并且没有工作(使purl成为一个唯一的名称)(同样它只更新purl,但不更新purl_full,或者更新它但是使其为空):

UPDATE localhost_campaigns.list_data 
SET purl = CASE purl 
WHEN 'temp_dingo' THEN 'dingo' 
WHEN 'temp_wombat' THEN 'wombat' 
WHEN 'temp_platypus' THEN 'platypus' 
WHEN 'temp_koala' THEN 'koala' 
END, 
purl_full = CASE purl 
WHEN 'temp_dingo' THEN 'http://dingo.domain.com' 
WHEN 'temp_wombat' THEN 'http://wombat.domain.com' 
WHEN 'temp_platypus' THEN 'http://platypus.domain.com' 
WHEN 'temp_koala' THEN 'http://koala.domain.com' 
END 
WHERE (purl IN ('temp_dingo', 'temp_wombat', 'temp_platypus', 'temp_koala') AND cid = 2050); 

作为一个附带问题,它如何进行更新?它是否完成第1列的所有第2列,或同时执行两列,然后转到下一条记录并同时执行两列?

1 个答案:

答案 0 :(得分:0)

这是最终起作用的。我需要将第二个SET CASE中的purl名称更改为它所更改的名称,而不是原始名称。所以那种问题的答案是否会一次一列或一次排列。

UPDATE localhost_campaigns.list_data 
SET purl = CASE purl 
WHEN 'temp_dingo' THEN 'dingo' 
WHEN 'temp_wombat' THEN 'wombat' 
WHEN 'temp_platypus' THEN 'platypus' 
WHEN 'temp_koala' THEN 'koala' 
END, 
purl_full = CASE purl 
WHEN 'dingo' THEN 'http://dingo.domain.com' 
WHEN 'wombat' THEN 'http://wombat.domain.com' 
WHEN 'platypus' THEN 'http://platypus.domain.com' 
WHEN 'koala' THEN 'http://koala.domain.com' 
END 
WHERE (purl IN ('temp_dingo', 'temp_wombat', 'temp_platypus', 'temp_koala') AND cid = 2050);