2017-10-13 147 views
0

我有一個蘭德公司與限制,順序選擇,所以我得到的每status的集數再搭配整體結果得到一個隨機結果不是對每個status塊這樣的SQL查詢,並按照預期運行剛:SQL聯盟更新

(SELECT * FROM `vicidial_list` WHERE phone_number LIKE "7%" AND list_id NOT IN (500,1000) AND status = "AR" LIMIT 1038) 
UNION 
(SELECT * FROM `vicidial_list` WHERE phone_number LIKE "7%" AND list_id NOT IN (500,1000) AND status = "CBHOLD" LIMIT 372) 
UNION 
(SELECT * FROM `vicidial_list` WHERE phone_number LIKE "7%" AND list_id NOT IN (500,1000) AND status = "NEW" LIMIT 3824) 
UNION 
(SELECT * FROM `vicidial_list` WHERE phone_number LIKE "7%" AND list_id NOT IN (500,1000) AND status = "PI" LIMIT 1405) 
UNION 
(SELECT * FROM `vicidial_list` WHERE phone_number LIKE "7%" AND list_id NOT IN (500,1000) AND status = "NRP" LIMIT 500) 
UNION 
(SELECT * FROM `vicidial_list` WHERE phone_number LIKE "7%" AND list_id NOT IN (500,1000) AND status = "RCAT" LIMIT 500) 
ORDER BY RAND() 

現在我想它來更新list_id到200,但是這並不工作:

(UPDATE `vicidial_list` SET list_id = 200 WHERE phone_number LIKE "7%" AND list_id NOT IN (500,1000) AND status = "AR" LIMIT 1038) 
UNION 
(UPDATE `vicidial_list` SET list_id = 200 WHERE phone_number LIKE "7%" AND list_id NOT IN (500,1000) AND status = "CBHOLD" LIMIT 372) 
UNION 
(UPDATE `vicidial_list` SET list_id = 200 WHERE phone_number LIKE "7%" AND list_id NOT IN (500,1000) AND status = "NEW" LIMIT 3824) 
UNION 
(UPDATE `vicidial_list` SET list_id = 200 WHERE phone_number LIKE "7%" AND list_id NOT IN (500,1000) AND status = "PI" LIMIT 1405) 
UNION 
(UPDATE `vicidial_list` SET list_id = 200 WHERE phone_number LIKE "7%" AND list_id NOT IN (500,1000) AND status = "NRP" LIMIT 500) 
UNION 
(UPDATE `vicidial_list` SET list_id = 200 WHERE phone_number LIKE "7%" AND list_id NOT IN (500,1000) AND status = "RCAT" LIMIT 500) 
ORDER BY RAND() 

有什麼建議?

謝謝。

+4

沒有工會與UPDATE – Serg

回答

2

只要運行每個UPDATE作爲一個單獨的語句:

UPDATE `vicidial_list` SET list_id = 200 WHERE phone_number LIKE "7%" AND list_id NOT IN (500,1000) AND status = "AR" ORDER BY RAND() LIMIT 1038; 
UPDATE `vicidial_list` SET list_id = 200 WHERE phone_number LIKE "7%" AND list_id NOT IN (500,1000) AND status = "CBHOLD" ORDER BY RAND() LIMIT 372; 
UPDATE `vicidial_list` SET list_id = 200 WHERE phone_number LIKE "7%" AND list_id NOT IN (500,1000) AND status = "NEW" ORDER BY RAND() LIMIT 3824; 
UPDATE `vicidial_list` SET list_id = 200 WHERE phone_number LIKE "7%" AND list_id NOT IN (500,1000) AND status = "PI" ORDER BY RAND() LIMIT 1405; 
UPDATE `vicidial_list` SET list_id = 200 WHERE phone_number LIKE "7%" AND list_id NOT IN (500,1000) AND status = "NRP" ORDER BY RAND() LIMIT 500; 
UPDATE `vicidial_list` SET list_id = 200 WHERE phone_number LIKE "7%" AND list_id NOT IN (500,1000) AND status = "RCAT" ORDER BY RAND() LIMIT 500; 
+0

謝謝,但對於ORDER BY RAND()洗牌 – medk

+1

您需要添加'ORDER BY RAND()'每'UPDATE'。我更新了我的答案。 –

+0

是的,我知道,但我的意思是洗牌整個最終結果,如我發佈的第一個代碼的輸出,而不是將每個更新洗牌。 – medk