また当月内のキャンペーン期間の該当日数の合計を求めたい。
おぼえがき
CASE文で集約する
SELECT SUM( CASE WHEN LAST_DAY(CURDATE()) = LAST_DAY(start_date) THEN DATEDIFF(LAST_DAY(CURDATE()), start_date) WHEN DATE_FORMAT(LAST_DAY(CURDATE() - INTERVAL 1 MONTH),"%Y%m") = DATE_FORMAT(start_date,"%Y%m") THEN 30 - DATEDIFF(LAST_DAY(CURDATE() - INTERVAL 1 MONTH ), start_date) ELSE 0 END ) as days