MySQL按日期分组()

时间:2015-02-25 00:27:32

标签: mysql

嘿,我创建了一个更大的查询来计算收益率和余额。我用这个数据创建折线图。但是,我觉得将所有这些都放在折线图中而不是想要组或将具有相同日期的记录合并在一起是非常笨拙的。基本上它应该只显示每天的最后一条记录。我想要的输出的插图。权利是当前的。左边是我想要的。正如您所看到的那样,左侧已删除了每天不是最后一条的所有记录。

enter image description here

SET @balance = 0;
SET @yield = 0;

SELECT *, 
    CASE WHEN stats =  'wrong'
    THEN @balance := @balance -100
    ELSE @balance := @balance + ( odds * 100 - 100 ) 
    END as balance,
    CASE WHEN stats =  'wrong'
            THEN @yield := @yield + ( 0 / 100) *100
            ELSE @yield := @yield + ( odds / 100) * 100
    END as yield
    FROM (SELECT odds.meta_value AS odds, stat.meta_value AS stats, posts.post_date
            FROM wp_t3a673_posts posts
            LEFT JOIN wp_t3a673_postmeta stat ON posts.ID = stat.post_id
            AND stat.meta_key =  'status'
            LEFT JOIN wp_t3a673_postmeta odds ON posts.ID = odds.post_id
            AND odds.meta_key =  'odds'
            LEFT JOIN wp_t3a673_term_relationships tr ON posts.ID = tr.object_id
            LEFT JOIN wp_t3a673_term_taxonomy t ON tr.term_taxonomy_id = t.term_taxonomy_id
            WHERE (
            stat.meta_value =  'correct'
            OR stat.meta_value =  'wrong'
            )
            AND posts.post_status =  'publish'
            AND t.taxonomy =  'category'
            AND (
            t.term_id =4
            OR t.term_id =5
            OR t.term_id =6
            )
    ORDER BY posts.post_date
    ) tt

PHP

foreach ($balanceQuery as $row) 
{ 

    $value = $row->balance;


    $datetime = strtotime($row->post_date) * 1000;
    $data[] = "[$datetime, $value]";
}

0 个答案:

没有答案