数据表中不存在的月份也要显示,建议创建一个从1到12月份的表作为比对表。如果不方便创建月份比对表则可以用select 1到12的办法来虚拟这个月份比对表,但是语句会有些冗长。请参考下列写法:
select months.year,months.month,concat(ifnull(t.sumMonthNum,'0'),
'/',months.sumYearNum) as `月/年比`
from
(select * from
(select year(time) as year,
sum(num) as sumYearNum
from abc group by year(time)) years,
(select 1 as month union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select 8 union all
select 9 union all
select 10 union all
select 11 union all
select 12) months) months left join
(select year(time) as year,
month(time) as month,
sum(num) as sumMonthNum
from abc group by year(time),month(time)) t
on months.month=t.month
order by months.year,months.month
实验截图如下:
源表数据
SQL代码及运行结果
SELECTTIMESTAMPDIFF(
YEAR,
NOW(),
DATE_ADD(NOW(), INTERVAL 365 DAY)
),
TIMESTAMPDIFF(
MONTH,
NOW(),
DATE_ADD(NOW(), INTERVAL 365 DAY)
),
TIMESTAMPDIFF(
DAY,
NOW(),
DATE_ADD(NOW(), INTERVAL 365 DAY)
),
NOW(),
DATE_ADD(NOW(), INTERVAL 365 DAY)
首先要考虑给数据库减压,意思就是让数据库做最简单的事情。你可以把条件在php里边组装好,然后让mysql只单一的执行查询就好了,php的时间代码给你贴一下吧
<?phpdate_default_timezone_set('Asia/Shanghai')
echo "今天:".date("Y-m-d H:i:s")."<br>"
echo "昨天:".date("Y-m-d",strtotime("-1 day")), "<br>"
echo "明天:".date("Y-m-d",strtotime("+1 day")). "<br>"
echo "一周后:".date("Y-m-d",strtotime("+1 week")). "<br>"
echo "一周前:".date("Y-m-d",strtotime("-1 week")). "<br>"
echo "一周零两天四小时两秒后:".date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")). "<br>"
echo "下个星期四:".date("Y-m-d",strtotime("next Thursday")). "<br>"
echo "上个周一:".date("Y-m-d",strtotime("last Monday"))."<br>"
echo "一个月前:".date("Y-m-d",strtotime("last month"))."<br>"
echo "一个月后:".date("Y-m-d",strtotime("+1 month"))."<br>"
echo "十年后:".date("Y-m-d",strtotime("+10 year"))."<br>"
echo '<hr/>'
//php获取今日开始时间戳和结束时间戳
$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'))
$endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1
echo "今日开始时间戳和结束时间戳",'开始:',$beginToday,'结束:',$endToday,'<br/>'
echo "今日开始时间:",date("Y-m-d H:i:s",$beginToday),'<br/>'
echo "今日结束时间:",date("Y-m-d H:i:s",$endToday),'<br/>'
echo '<hr/>'
//php获取昨日起始时间戳和结束时间戳
$beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'))
$endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1
echo "昨日开始时间戳和结束时间戳",'开始:',$beginYesterday,'结束:',$endYesterday,'<br/>'
echo "昨日开始时间:",date("Y-m-d H:i:s",$beginYesterday),'<br/>'
echo "昨日结束时间:",date("Y-m-d H:i:s",$endYesterday),'<br/>'
echo '<hr/>'
//php获取上周起始时间戳和结束时间戳
$beginLastweek=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'))
$endLastweek=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'))
echo "上周开始时间戳和结束时间戳",'开始:',$beginLastweek,'结束:',$endLastweek,'<br/>'
echo "上周开始时间:",date("Y-m-d H:i:s",$beginLastweek),'<br/>'
echo "上周结束时间:",date("Y-m-d H:i:s",$endLastweek),'<br/>'
echo '<hr/>'
//php获取本月起始时间戳和结束时间戳
$beginThismonth=mktime(0,0,0,date('m'),1,date('Y'))
$endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'))
echo "本月开始时间戳和结束时间戳",'开始:',$beginThismonth,'结束:',$endThismonth,'<br/>'
echo "本月开始时间:",date("Y-m-d H:i:s",$beginThismonth),'<br/>'
echo "本月结束时间:",date("Y-m-d H:i:s",$endThismonth),'<br/>'
?>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)