-- 事前にDECLAREで変数定義 DECLARE start_date TIMESTAMP; DECLARE end_date TIMESTAMP; -- 変数に値を設定 SET start_date = TIMESTAMP('2019-01-01 00:00:00'); SET end_date = TIMESTAMP('2020-01-01 00:00:00'); SELECT start_date , end_date -- start_dateからend_dateまでの日数を取得 , DATE_DIFF( DATE(end_date), DATE(start_date) , DAY) as n_days -- start_dateからend_dateまでの週数を取得 , DATE_DIFF( DATE(end_date), DATE(start_date) , WEEK) as n_weeks -- start_dateからend_dateまでの月数を取得 , DATE_DIFF( DATE(end_date), DATE(start_date) , MONTH) as n_months
◆実行結果