需求:
從張貼主檔及明細檔,查找出當日申請者,將過帳檔對應的 id + postdate 的張貼數量加1。
SQL 語句:
UPDATE custom_data_1020 as e,
(SELECT b.data_id, b.ad_id, a.postdate_begin, a.postdate_end
from custom_data_1014_ad_list b
join custom_data_1014 a on a.data_id = b.data_id
WHERE b.data_id IN (SELECT data_id from custom_data_1014 where application_date = DATE_FORMAT(now(),"%Y-%m-%d"))
) as d
SET e.post_qty = e.post_qty + 1
WHERE e.id = d.ad_id and (postdate BETWEEN d.postdate_begin and d.postdate_end)
UPDATE custom_data_1020 SET post_qty=post_qty + 1
WHERE id in
(SELECT ad_id
FROM custom_data_1014_ad_list a, custom_data_1014 b
WHERE a.data_id = b.data_id AND (postdate BETWEEN b.postdate_begin AND b.postdate_end)
AND b.application_date = DATE_FORMAT(now(),"%Y-%m-%d")) 作者: u48326 時間: 2020-7-8 05:55 PM
感謝回覆。
我是初學 MySQL ,覺得它的應用真是千變萬化.....真讓人頭疼!作者: u48326 時間: 2020-7-9 11:47 AM
update custom_data_1020 P,
(
SELECT receipt_no, application_date, postdate_begin, postdate_end, ad_id, L.postdate, L.id, count(*) count
from custom_data_1014 M
join custom_data_1014_ad_list D on D.data_id=M.data_id
join custom_data_1020 L on L.id=D.ad_id and L.postdate between M.postdate_begin and M.postdate_end
where M.application_date=DATE_FORMAT(now(),"%Y-%m-%d")
GROUP BY id, postdate
order by id, postdate
) as N
set P.post_qty=P.post_qty+N.count
where P.id = N.id and P.postdate = N.postdate