简体   繁体   English

如何在mysql中更改一天的时间?

[英]How to change the time with one day plus in mysql?

I am using CodeIgniter and I need to get daily sales details by date. 我正在使用CodeIgniter,我需要按日期获取每日销售详细信息。 Below is my MySQL query statement, and in it I need to take values everyday from 15:00:00 to 06:00:00 the next morning (ie, today's date is 29-9-2015, and for this result I need the data to be from 29-9-2015 15:00:00 to 30-9-2015 06:00:00). 以下是我的MySQL查询语句,其中我需要每天第二天早上15:00:00到06:00:00取值(即,今天的日期是2015年9月29日,为此,我需要数据从2015年9月29日15:00:00到2015年9月30日06:00:00)。

I have tried so many ways. 我尝试了很多方法。 It is working only based on the daily hours from 15:00:00 to 24:00:00. 它仅根据每天15:00:00到24:00:00的时间运行。 If I give 06 instead of 24 then this does not work because the 06 is the next day's hour. 如果我给06而不是24,那么这将不起作用,因为06是第二天的时间。 I don't know how to +1 the day with hours in MySQL. 我不知道如何在MySQL中用小时数来为+1。

SELECT DATE_FORMAT( date, '%e' ) AS date, SUM( COALESCE( total_tax, 0 ) ) AS tax1, SUM( COALESCE( total_tax2, 0 ) ) AS tax2, SUM( COALESCE( total, 0 ) ) AS total, SUM( COALESCE( inv_discount, 0 ) ) AS discount, SUM( COALESCE( cash_amt, 0 ) ) AS cash, SUM( COALESCE( master_amt, 0 ) ) AS master, SUM( COALESCE( visa_amt, 0 ) ) AS visa, SUM( COALESCE( nets_amt, 0 ) ) AS nets, SUM( COALESCE( coupon_amt, 0 ) ) AS coupon, SUM( COALESCE( paid, 0 ) ) AS paid FROM sales WHERE (DATE_FORMAT( date, '%Y-%m' ) = '2015-09') AND (DELETE_STATUS != 'Deleted') AND DATE_FORMAT( DATETIME, '%H' ) BETWEEN '15' AND '24' GROUP BY DATE_FORMAT( DATETIME, '%e' );

I have attached my output screenshot with the result of 15 to 24 hours. 我将输出屏幕截图附加了15到24小时的结果。 I think you got my question. 我想你有我的问题。

在此处输入图片说明

to add day(s) you need: 添加您需要的日期:

SELECT DATE_ADD(date,INTERVAL 1 DAY)

to add hour(s) you need: 增加小时,您需要:

SELECT DATE_ADD(date,INTERVAL 1 HOUR)

You can also use DATE_SUB() instead of DATE_ADD() if you want to subtract instead of add time. 如果要减去而不是加时间,也可以使用DATE_SUB()代替DATE_ADD()。

full manual here: http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_date-add 完整的手册在这里: http : //dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_date-add

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM