mysql查询一周内每天的记录数。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql查询一周内每天的记录数。相关的知识,希望对你有一定的参考价值。
自己写的查询语句,目的就是统计这一周每天的记录数有多少条。感觉这样写的方法好像有点笨吧。有什么好的办法没。 $count1= mysql_num_rows(mysql_query("SELECT * FROM members WHERE TO_DAYS( NOW( ) ) - TO_DAYS( regtime)=6"));$count2= mysql_num_rows(mysql_query("SELECT * FROM members WHERE TO_DAYS( NOW( ) ) - TO_DAYS( regtime)=5"));$count3= mysql_num_rows(mysql_query("SELECT * FROM members WHERE TO_DAYS( NOW( ) ) - TO_DAYS( regtime)=4"));$count4= mysql_num_rows(mysql_query("SELECT * FROM members WHERE TO_DAYS( NOW( ) ) - TO_DAYS( regtime)=3"));$count5= mysql_num_rows(mysql_query("SELECT * FROM members WHERE TO_DAYS( NOW( ) ) - TO_DAYS( regtime)=2"));$count6= mysql_num_rows(mysql_query("SELECT * FROM members WHERE TO_DAYS( NOW( ) ) - TO_DAYS( regtime)=1"));$count7= mysql_num_rows(mysql_query("SELECT * FROM members WHERE TO_DAYS( NOW( ) ) - TO_DAYS( regtime)=0"));
假设members 表里有个字段是日期(CDATA),和一个ID字段,要统计每天的条数,可以用下面的语句:SELECT i.CDATA,count(*) from members i
where cdata>=date_sub(now(),interval dayofweek(now()) day) and cdata <= date_add(now(),interval 7-dayofweek(now()) day) group by i.CDATA
以上语句统计的是从这周的第一天(星期天)到这周的最后一天(星期六)每天的记录条数。
程序里取出来循环得到每天的条数(查询出来的结果已经按日期分好,只要读取就行) 参考技术A $re = mysql_query("SELECT count(*) as n, (TO_DAYS( NOW( ) ) - TO_DAYS( regtime)) as `d` FROM members group by TO_DAYS( regtime) having d <= 6");
while($rc = mysql_fetch_assoc($re))
echo $rc['d']; //这个 0 代表今天 1 代表 1天前 2代表2天前 。。。
echo '<br />';
echo $rc['n']; //这个是数量
参考技术B 可以循环SELECT * FROM members WHERE TO_DAYS( NOW( ) ) - TO_DAYS( regtime)=XXX啊。。 参考技术C come here to see
从mysql获取数据并在几周内对它们进行分组,并显示日期
我每周打印数据。我需要在查询中添加一个东西。我想要一周的开始和结束日期以及数据。
目前数据是这样的
[0] => Array
(
[WEEK] => 7
[total_sub_total] => 110.30
[total_tax] => 9.92
[total_restaurant_delivery_fee] => 0
)
[0] => Array
(
[WEEK] => 8
[total_sub_total] => 6540.00
[total_tax] => 1046.40
[total_restaurant_delivery_fee] => 0
)
我想要那个星期的日期。例如,它应该是
[0] => Array
(
[WEEK] => 8,
[starting_week_date] => 2018-02-21 13:18:10,
[ending_week_date] => 2018-02-26 13:18:10,
[total_sub_total] => 6540.00
[total_tax] => 1046.40
[total_restaurant_delivery_fee] => 0
)
这是代码
public function getWeeklyEarnings($restaurant_id)
{
return $this->find('all', array(
//'contain' => array('OrderMenuItem', 'Restaurant', 'OrderMenuItem.OrderMenuExtraItem', 'PaymentMethod', 'Address','UserInfo','RiderOrder.Rider'),
'conditions' => array(
'Order.restaurant_id' => $restaurant_id,
'Order.status' => 2,
'Order.created > DATE_SUB(NOW(), INTERVAL 4 WEEK)'
),
'fields' => array(
'WEEK(Order.created) AS WEEK',
'sum(Order.sub_total) AS total_sub_total',
'sum(Order.tax) AS total_tax',
'sum(Order.restaurant_delivery_fee) AS total_restaurant_delivery_fee',
),
'group' => array('WEEK(Order.created)'),
'recursive' => 0
));
}
以上是关于mysql查询一周内每天的记录数。的主要内容,如果未能解决你的问题,请参考以下文章