問題:
今天第一次接觸報表,各種對時間的需求聯合查詢,相當棘手,如下圖需求
於是,各種搜查相關mysql的時間sql查詢語句並整理
1.查詢今天時間/日期/
select now();
select Date(NOW());
2.查詢當前月份/當前月份第一天/上個月的最後一天
SELECT MONTH(NOW()) time;
SELECT concat(date_format(LAST_DAY(now()),'%Y-%m-'),'01') time;
select date_sub(date_sub(date_format(now(),'%y-%m-%d'),interval extract(
day from now()) day),interval 0 month) as date;
3.列出本月月初到現在所有日期
SELECT DISTINCT date_format(
DATE_ADD(
(select date_sub(date_sub(date_format(now(),'%y-%m-%d'),interval extract(
day from now()) day),interval 0 month) as date), INTERVAL id DAY),'%Y-%m-%d') AS datetime
FROM '表名' zt WHERE //隨便查詢一個表
DATE_ADD((select date_sub(date_sub(date_format(now(),'%y-%m-%d'),interval extract(
day from now()) day),interval 0 month) as date),INTERVAL id DAY) <= (select Date(NOW()))
4.和其它依靠時間字段查詢結果的表拼接結果時,用left join關鍵字
select * from
(select * 結果1) as a
left join
(select * 結果2) as b
on a.datetime =b.time