以PHP为单位获取每日销售总额

时间:2011-10-15 06:32:54

标签: php mysql charts

我以前从未这样做过,所以我迷失了。

我有以下交易表:

transaction_id  transaction_user    transaction_ammount transaction_time
1                     2                   950              1318482908
2                     2                   750              1318482916
3                     2                   950              1318482939

然后,我需要创建一个查询,这有助于我提取每周数据,按天分组,这样我就可以创建一个图表,你能得到我吗?

到目前为止,我已经在PHP中检测到周数并打印完整的工作日(我使用phpbb3):

    $format = 'l'; 
    $ts = time();   // set current Unix timestamp
    $today = date($format, $ts); // set today
    $year = date('o', $ts);
    $week = date('W', $ts);
    $weekdays = 7; 


    for($i = 1; $i <= $weekdays; $i++) 
    {
        // timestamp from ISO week date format
        $ts = strtotime($year.'W'.$week.$i);

        $day = date($format, $ts);
        // test if $day is $today
        if ($day == $today) {
            $day = '<strong>' . $day . '</strong>';
        }

        $template->assign_block_vars('time', array(
            'DATE'                  => $day,
        ));
    }

现在,我想安排星期一,星期二等发生的总销售额......等等......

1 个答案:

答案 0 :(得分:1)

这个mysql查询

select week(transaction_time) as weekno, weekday(transaction_time) as dayno, sum(transaction_ammount) as total_amount
group by     week(transaction_time), weekday(transaction_time)

会给你这个结果集

|weekno|dayno|total_amount

这里有一些对mysql中这些函数的引用,因此你可以解释数据

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_week

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_weekday

UPDATE,尝试这个sql,因为它可能需要转换时间戳以使其他功能正常工作

select week(FROM_UNIXTIME(transaction_time)) as weekno, weekday(FROM_UNIXTIME(transaction_time)) as dayno, sum(transaction_ammount) as total_amount
group by    week(FROM_UNIXTIME(transaction_time)), weekday(FROM_UNIXTIME(transaction_time))