常見sql語句集錦

2021-10-06 11:22:38 字數 1897 閱讀 7899

1.unix_timestamp() 自』1970-01-01 00:00:00』的到當前時間的秒數差

2.select unix_timestamp(『2012-06-08』) 獲取date 日期的時間戳

3.select *, if(unix_timestamp()1000-createtime>(select timeout from zxgd_rules limit 1)36001000,1,0) timestate from zxgd_task;

根據判斷條件新增字段

4.a.,

case when

timestampdiff(

hour,

from_unixtime( convert ( a.submittime / 1000, signed ), 「%y-%m-%d %h:%i:%s」 ),

date_format( now( ), 『%y-%m-%d %h:%i:%s』 )

) > b.timeout then 1 else 0 end as overtime_answer, – 超時響應時間判斷

case when

timestampdiff(

hour,

from_unixtime( convert ( a.submittime / 1000, signed ), 「%y-%m-%d %h:%i:%s」 ),

date_format( now( ), 『%y-%m-%d %h:%i:%s』 )

) > b.dubanout then 1 else 0 end as overtime, – 超時判斷

case when

timestampdiff(

hour,

from_unixtime( convert ( a.submittime / 1000, signed ), 「%y-%m-%d %h:%i:%s」 ),

date_format( now( ), 『%y-%m-%d %h:%i:%s』 )

) > b.linjieout then 1 else 0 end as overtime_divide – 臨界超時判斷

from

zxgd_task a,

zxgd_rules b

5.多種count的統計的條件語句的計算

sqlcmd += 『select a.userid,count(case when b.sumestipaid<=5000 or b.sumestipaid=nullif(b.sumestipaid,1) then 1 else null end) as count1』;

sqlcmd += 『,count(0) as totalcount』;

sqlcmd += 『,count(if(b.sumestipaid<=5000 and (b.finishtime-a.reporttime)>86400000,true,null)) as count2』;

sqlcmd += 『,count(if(b.sumestipaid>5000 and b.sumestipaid<10000,true,null)) as count3』;

sqlcmd += 『,count(if(b.sumestipaid>5000 and b.sumestipaid<10000 and (b.finishtime-a.reporttime)>432000000,true,null)) as count4』

sqlcmd += 『,count(if(b.sumestipaid>10000,true,null)) as count5』;

sqlcmd += 』 from orders as a left join duban_caseinfo as b on a.registno=b.registno where userid !=0 and a.ordertime>= ? and a.ordertime<= ? group by userid;』;

SQL語句集錦

use tablename 要操作的資料庫名 select logicalfilename tablename log 日誌檔名 maxminutes 10,limit on time allowed to wrap log.newsize 1 你想設定的日誌檔案的大小 m setup initia...

Mysql 常用SQL語句集錦

查詢時間,友好提示 乙個sql 返回多個總數 sql select count all,sql count case when status 1 then status end status 1 num,sql count case when status 2 then status end sta...

sql語句編寫集錦 pgsql

1 查詢活動次數 1 活動次數查詢 select from select province,year,act num act people from bdd tmp ci order by year desc limit 5 as a1 order by act num desc 2 活動人數的查詢...