mysql语句统计天数,datediff函数的使用

来源:互联网 发布:数据库视频教程下载 编辑:程序博客网 时间:2024/06/05 06:39

因为要得到优惠券的有效天数,所以需要在sql语句中用到统计天数的函数datediff:select datediff(dd,start_time,end_time) from table;

但是写了之后发现报错Error Code: 1582. Incorrect parameter count in the call to native function 'datediff'

后来查了资料才知道各类型数据库统计天数的sql写法不同:

datediff函数在mysql下只有起始时间、结束时间2个参数,没有datepart参数,而我用的mysql,所以报错是因为我多写了dd,正确写法应该是select datediff(start_time,end_time) from table;

而带datepart参数是SQL Server的写法,在SQL Server中,datepart的值可以是yy,dd,mm,ww等,其对应的是统计年数,月数,天数,周数等;

还有如果是oracle,网上的方法是直接用两个时间进行相减,比如:select date1-date2 from table,或者select trunc(date1-date2) 天数 from table.