常用相關查詢sql語句 時間 排序

2021-09-24 02:43:10 字數 1909 閱讀 1641

時間

select from_unixtime(1493138520)
輸出為:2017-04-26 00:42:00

select unix_timestamp('2017-04-26 00:42:00','yyyy-mm-dd hh:mm:ss')
輸出為:1485362520

select from_unixtime(1545711900,'yyyy-mm-dd hh:mm:ss')
輸出為:2018-12-25 12:25:00

select date_sub('1998-01-01 00:42:00',1)
輸出為:1997-12-31

select date_add(@dt, interval '1 01:15:30' day_second);
select datediff('2019-04-01','2019-03-28')
輸出為:4

排序

row_number會為查詢出來的每一行記錄生成乙個序號,依次排序且不會重複,注意使用row_number函式時必須要用over子句選擇對某一列進行排序才能生成序號。row_number用法例項:

圖中的row_num列就是row_number函式生成的序號列,其基本原理是先使用over子句中的排序語句對記錄進行排序,然後按照這個順序生成序號。如以下sql,over子句中根據subtime降序排列,sql語句中則按totalprice降序排列。

row_number() over(partition by dt,uuid order by page_ts desc) as rank
rank函式用於返回結果集的分區內每行的排名, 行的排名是相關行之前的排名數加一。簡單來說rank函式就是對查詢出來的記錄進行排名,與row_number函式不同的是,rank函式考慮到了over子句中排序字段值相同的情況,如果使用rank函式來生成序號,over子句中排序字段值相同的序號是一樣的,後面字段值不相同的序號將跳過相同的排名號排下乙個,也就是相關行之前的排名數加一,可以理解為根據當前的記錄數生成序號。

dense_rank函式在生成序號時是連續的,出現相同排名時,將不跳過相同排名號,rank值緊接上一次的rank值。

SQL語句 時間相關

1 獲得 sql server 中系統時間 語句 select getdate 例項 update telecomam pand dbo requisitioninfo set submittime select getdate where uniqueid 6fa2912d 1bf6 4ecc 9...

sql時間查詢語句

很有用的sql語句。1.查詢當天 select from table where datediff dd,dateadd getdate 0 2.查詢24小時內的 select from table where datediff hh,dateadd getdate 24 3.本月記錄 select...

sql語句,日期時間相關

修改時間段,將此時間段內資料的時間增加乙個月插入到表裡 壩頂沉降 insert into dtjc bd bd sbbh,bd y,bd time select bd sbbh,bd y,date add bd time,interval 1month from dtjc bd c where c....