2012-07-02 77 views
-1

我有一個MySQL查詢選擇了多項紀錄MySQL的更新選擇

SELECT 
leads.Affiliate_ID, 
leads.first_name, 
leads.last_name, 
leads.email, 
leads.loan_purpose, 
leads.home_tel, 
leads.mobile_tel, 
leads.postcode, 
leads.house_num, 
leads.app_time, 
leads.new_application_date, 
leads.payment_accepted, 
leads.sms 
FROM 
leads 
WHERE 
leads.Affiliate_ID = '23' AND 
leads.sms IS NULL AND 
leads.payment_accepted IS NULL AND 
leads.new_application_date < (NOW() - INTERVAL 5 DAY) 

我怎麼現在在所有選定的記錄更新leads.sms1

任何幫助或建議將是巨大

回答

0

更新數據,你並不需要選擇使用select語句的記錄。您需要使用WHERE子句將條件如下所示。使用WHERE子句,對滿足該條件的行更新記錄。

在你的問題
UPDATE 
leads SET sms = 1 
WHERE 
Affiliate_ID = '23' AND 
sms IS NULL AND 
payment_accepted IS NULL AND 
new_application_date < (NOW() - INTERVAL 5 DAY) 

此外,在您使用leads.FIELD_NAME所有地方。當有一個表格時,您不需要在字段名稱前使用表格名稱。當使用兩個或更多表格使用JOINS時,需要表格名稱。

0

只需在標準的地方替換之前的所有東西update syntax

update leads set sms=1 
WHERE 
leads.Affiliate_ID = '23' AND 
leads.sms IS NULL AND 
leads.payment_accepted IS NULL AND 
leads.new_application_date < (NOW() - INTERVAL 5 DAY) 
+0

感謝所以它應該看起來像SELECT leads.Affiliate_ID, leads.first_name, leads.last_name, leads.email, leads.loan_purpose, leads.home_tel, leads.mobile_tel, leads.postcode, leads.house_num, leads.app_time, leads.new_application_date, leads.payment_accepted, leads.sms FROM leads update leads set sms=1 WHERE leads.Affiliate_ID = '23' AND leads.payment_accepted IS NULL AND leads.sms IS NULL AND leads.new_application_date < (NOW() - INTERVAL 5 DAY)或者我應該做我的選擇和prepossessing然後運行一個新的mysql命令來更新 – user1053865

+0

只是運行更新命令,它會更新所有選定的行,所以下一次執行選擇它應該返回沒有行導致sms列已更新(不再爲空)。 –

+0

順便說一下,這是一個很好的做法,就像在更新之前對選擇查詢所做的那樣進行測試,因此您可以確定要更改哪些行。 –

2

如果我明白你的問題的話,試着:

UPDATE 
leads SET sms = 1 
WHERE 
Affiliate_ID = '23' AND 
sms IS NULL AND 
payment_accepted IS NULL AND 
new_application_date < (NOW() - INTERVAL 5 DAY) 
+0

所以我運行後,如果已經處理記錄 – user1053865

+0

更新命令不依賴於選擇命令,所以你可以隨時運行它,嘗試先運行你的選擇,觀察結果,然後運行更新並運行選擇再次觀察結果。 –