請問mysql的sql中如何計算兩個datetime的差精確

2022-03-02 20:24:41 字數 5181 閱讀 5667

1樓:匿名使用者

timestampdiff(interval,datetime_expr1,datetime_expr2)

返回日期或日期時間表示式datetime_expr1 和datetime_expr2the 之間的整數差。

其結果的單位由interval 引數給出。該引數必須是以下值的其中一個:

frac_second 表示間隔是毫秒

second  秒

minute  分鐘

hour  小時

day  天

week  星期

month  月

quarter  季度

year  年

select '年'  as  `日期部分`,  timestampdiff(year, '2012-12-21',  current_timestamp()) as `數值`

union all

select '季度'  as  `日期部分`,  timestampdiff(quarter, '2012-12-21', current_timestamp()) as `數值`

union all

select '月'  as  `日期部分`,  timestampdiff(month, '2012-12-21', current_timestamp()) as `數值`

union all

select '日'  as  `日期部分`,  timestampdiff(day, '2012-12-21', current_timestamp()) as `數值`

union all

select '周'  as  `日期部分`,   timestampdiff(week, '2012-12-21', current_timestamp()) as `數值`

union all

select '時'  as  `日期部分`,   timestampdiff(hour, '2012-12-21', current_timestamp()) as `數值`

union all

select '分'  as  `日期部分`,   timestampdiff(minute, '2012-12-21', current_timestamp()) as `數值`

union all

select '秒'  as  `日期部分`,   timestampdiff(second, '2012-12-21', current_timestamp()) as `數值`

;+----------+----------+

| 日期部分 | 數值     |

+----------+----------+

| 年       |        1 |

| 季度     |        4 |

| 月       |       12 |

| 日       |      388 |

| 周       |       55 |

| 時       |     9328 |

| 分       |   559737 |

| 秒       | 33584279 |

+----------+----------+

8 rows in set (0.00 sec)

mysql> select current_timestamp();

+---------------------+

| current_timestamp() |

+---------------------+

| 2014-01-13 16:58:17 |

+---------------------+

1 row in set (0.00 sec)

2樓:萬菡

精確到小時

select timestampdiff(hour,'2014-01-01 14:00','2014-01-01 16:00');

精確到分鐘

select timestampdiff(minute,'2014-01-01 14:00','2014-01-01 16:00')

關於資料庫mysql中求兩個時間差的問題。 50

3樓:隨風潛入夜

mysql計算兩個日期的時間差函式timestampdiff

例 select timestampdiff(hour, '2010-04-23 17:53:38', '2010-04-22 15:49:43')

4樓:厙曼冬

select datediff(max(datecol),min(daecol)) from table1

利用sql語句如何獲得兩個日期之間相差的天數

5樓:大野瘦子

用sysdate假設結束日期欄位是end_date

新增這個判斷條件:

where to_char("end_date",'yyyy') = to_char(sysdate,'yyyy') 判斷年相同

and to_char("end_date",'mm') = to_char(sysdate,'mm') 判斷月相同

and to_char("end_date",'dd') - to_char(sysdate,'dd') = 15 判斷日相同

或者:where to_char("end_date",'yyyy-mm-dd') - to_char(sysdate,'yyyy-mm-dd')=15

6樓:匿名使用者

可以用datediff函式。

建立表及插入資料:

create table test

(begindate datetime,

enddate datetime);

insert into test values ('2015-01-01','2015-07-13')

執行:select datediff(day,begindate,enddate) from test;結果:

7樓:匿名使用者

求兩個日期(date1、date2)之間相差的天數用datediff函式,返回值為數值型,可以用cast函式或者convert函式

declare @a datetime

set @a ='2013-04-09'

select cast((datediff(day,@a,getdate()))as int)

結果為:7

你的需求sql語句為

select cast((datediff(day,date1,date2 ))as int)

或者可以這樣select convert(int,date2)-cast(date1 as int)

8樓:匿名使用者

select datediff(dd,'[輸入的時間]',getdate()) <*****datediff()函式去看看吧,能對資料庫的時間操作的。dd表示的是日期。

9樓:匿名使用者

用sysdate 假設結束日期欄位是 end_date

那麼就新增這個判斷條件

where to_char("end_date",'yyyy') = to_char(sysdate,'yyyy') 判斷年相同

and to_char("end_date",'mm') = to_char(sysdate,'mm') 判斷月相同

and to_char("end_date",'dd') - to_char(sysdate,'dd') = 15 判斷日相同

當然 不知道這樣是否可行

where to_char("end_date",'yyyy-mm-dd') - to_char(sysdate,'yyyy-mm-dd')=15

你都可以試一下 希望能夠幫助你

10樓:匿名使用者

select datediff(dd,'輸入的日期',getdate())

11樓:匿名使用者

select datediff(day,'date1',getdate())

12樓:斗轉參橫

select datediff(dd,date1,date2)

13樓:匿名使用者

select datediff(d,時間一,時間二)

14樓:匿名使用者

**這麼費勁啊 mysql的話 直接用unix_timestamp將時間轉換為時間戳 直接相減就好了啊

在sql語句中怎樣計算出兩個日期的差值

15樓:匿名使用者

在sql語句中怎樣計算出兩個日期的差值使用datediff函式

一、函式功能:datediff() 函式返回兩個日期之間的回間隔答時間。

二、語法:datediff(datepart,startdate,enddate)

startdate 和 enddate 引數是合法的日期表示式。

datepart 引數可以是下列的值:

三、例項演示

獲取日期'2008-12-29'與'2008-12-30'之間間隔的天數(參考datepart 引數**,dd表示獲取間隔的天數)

select datediff(dd,'2008-12-29','2008-12-30') as diffdate結果:

16樓:匿名使用者

select datediff(day,cast('2009-06-17' as datetime),cast('2009-06-23' as datetime))

在mysql中,有個時間欄位型別是datetime型別的。如何寫sql,輸出24小時之內的資料

17樓:匿名使用者

select * from 表名 where 時間欄位》date_add(now(), interval -1 day);

18樓:匿名使用者

unix_timestamp(datetime)-unix_timestamp(1)<24*60*60; 根據時間戳來計算。

19樓:熊貓的朋友

取出整個資料,然後在擷取字串

如何查詢MySQL中查詢慢的SQL語句

mysql資料庫有幾個配置選項可以幫助我們及時捕獲低效sql語句。1,slow query log 這個引數設定為on,可以捕獲執行時間超過一定數值的sql語句。2,long query time 當sql語句執行時間超過此數值時,就會被記錄到日誌中,建議設定為1或者更短。3,slow query ...

MySQL怎麼查詢比較耗時的sql語句

一 mysql資料庫有幾個配置選項可以幫助我們及時捕獲低效sql語句1,slow query log 這個引數設定為on,可以捕獲執行時間超過一定數值的sql語句。2,long query time 當sql語句執行時間超過此數值時,就會被記錄到日誌中,建議設定為1或者更短。3,slow query...

sql中如何給變數賦值,Sql中如何給變數賦值

declare n1 int,n2 varchar 10 set n1 select age from table where column set n2 select gender from table where column 或者一起賦值 就是樓上那個 declare n1 int,n2 va...