使用SQL語句來判斷資料庫中時間,是否為當日時間

2021-10-01 01:47:14 字數 3112 閱讀 7493

1.專案情景

這兩天寫功能時,有一條是使用者簽到,當天只能簽到一次。一直想的是將系統當前時間和資料庫中的最近時間進行比較。但具體怎麼處理資料庫中datatime型別的資料一直沒有頭緒。

2.sql語句解決

select count(1) from sign_detail  where date_format(create_time,'%y-%m-%d')= date_format(now(),'%y-%m-%d')  and member_id =
直接在sql中實現了將create_time和當前時間按年月日格式比較,若處在一條相同的·,則說明該使用者今日已簽到。

3.mysql判斷時間是否是當天,昨天

今天  

select * from 表名 where to_days(時間欄位名) = to_days(now());

昨天

select * from 表名 where to_days( now( ) ) - to_days( 時間欄位名) <= 1

7天

select * from 表名 where date_sub(curdate(), interval 7 day) <= date(時間欄位名)

近30天

select * from 表名 where date_sub(curdate(), interval 30 day) <= date(時間欄位名)

本月

select * from 表名 where date_format( 時間欄位名, '%y%m' ) = date_format( curdate( ) , '%y%m' )

上一月

select * from 表名 where period_diff( date_format( now( ) , '%y%m' ) , date_format( 時間欄位名, '%y%m' ) ) =1

#查詢本季度資料

select * from `ht_invoice_information` where quarter(create_date)=quarter(now());

#查詢上季度資料

select * from `ht_invoice_information` where quarter(create_date)=quarter(date_sub(now(),interval 1 quarter));

#查詢本年資料

select * from `ht_invoice_information` where year(create_date)=year(now());

#查詢上年資料

select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));

查詢當前這週的資料

select name,submittime from enterprise where yearweek(date_format(submittime,'%y-%m-%d')) = yearweek(now());

查詢上週的資料

select name,submittime from enterprise where yearweek(date_format(submittime,'%y-%m-%d')) = yearweek(now())-1;

查詢當前月份的資料

select name,submittime from enterprise where date_format(submittime,'%y-%m')=date_format(now(),'%y-%m')

查詢距離當前現在6個月的資料

select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();

查詢上個月的資料

select name,submittime from enterprise where date_format(submittime,'%y-%m')=date_format(date_sub(curdate(), interval 1 month),'%y-%m')

select * from ` user ` where date_format(pudate, ' %y%m ' ) = date_format(curdate(), ' %y%m ' ) ;

select * from user where weekofyear(from_unixtime(pudate,'%y-%m-%d')) = weekofyear(now())

select *

from user

where month (from_unixtime(pudate, ' %y-%m-%d ' )) = month (now())

select *

from [ user ]

where year (from_unixtime(pudate, ' %y-%m-%d ' )) = year (now())

and month (from_unixtime(pudate, ' %y-%m-%d ' )) = month (now())

select *

from [ user ]

where pudate between 上月最後一天

and 下月第一天

where date(regdate) = curdate();

select * from test where year(regdate)=year(now()) and month(regdate)=month(now()) and day(regdate)=day(now())

select date( c_instime ) ,curdate( )

from `t_score`

where 1

limit 0 , 30

資料庫SQL語句使用

擷取字段 string sgtoday,6,2 引數1 要擷取的字段,引數2 從第幾位擷取,引數3 擷取長度。例如 擷取folder no欄位從第一位開始的長度和fonds no相同的字段 select from folder description kj where substring folde...

資料庫sql判斷語句(case,IF)

select b.cat id,case when a.cat id null then count else 0 end as goods num from globals ecs table goods as a right join globals ecs table category as ...

通過SQL語句來備份,還原資料庫

這裡僅僅用到了一種方式而已,把資料庫檔案備份到磁碟然後在恢復.eg 1 2 通過sql 語句備份資料庫3 4 backup database mydb5 todisk c dbback mydb.bak 6 這裡指定需要備份資料庫的路徑和檔名,注意 路徑的資料夾是必須已經建立的.檔名可以使用日期來標...