在执行case语句之前命令sql查询

时间:2015-02-17 09:24:08

标签: mysql

我正在尝试计算我需要创建的图表的余额但是我已经遇到了数据在日期之后没有订购的问题,如果我按日期订购它会在我计算完之后这样做平衡,因此平衡仍然是错误的。如何在该日期之前订购查询后计算余额?

http://sqlfiddle.com/#!2/374b9/6

测试数据:

CREATE TABLE betting 
    (
     id int auto_increment primary key,
     date DATETIME, 
     odds varchar(20),
     status varchar(30)
    );

INSERT INTO betting
(id, date, odds, status)
VALUES
('1', '2015-02-08 20:27:44', '1.70', 'wrong'),
('2', '2015-02-08 13:22:17', '3.05', 'correct'),
('3', '2015-02-09 16:40:45', '2.20', 'correct'),
('4', '2015-02-10 13:58:24', '1.33', 'correct'),
('5', '2015-02-12 11:37:51', '1.35', 'correct'),
('6', '2015-02-12 10:24:13', '1.38', 'correct');


SET @balance = 0;
SELECT date, odds, status, CASE WHEN status =  'wrong'
            THEN @balance := @balance -100
            ELSE @balance := @balance + ( odds * 100 - 100 ) 
            END as balance
from betting

期望余额

205
105
225
258
293
331

我创建了一个SQLFiddle,使其更容易。


额外信息

由于存在一些混淆,我决定添加真正的查询,因为它是wordpress,所以它会更长一些。

    SET @balance = 0;
     SELECT odds.meta_value AS odds, stat.meta_value AS stats, posts.post_date, 
        CASE WHEN stat.meta_value =  'wrong'
        THEN @balance := @balance -100
        ELSE @balance := @balance + ( odds.meta_value * 100 - 100 ) 
        END as balance
                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

结果如下。在这里你可以清楚地看到第一个余额不正确:它应该是-100,但是返回105,这是因为它在计算余额后订单:

enter image description here

如果我只是-100,如果它是错误或正确的,你甚至可以清楚地看到问题

enter image description here

2 个答案:

答案 0 :(得分:0)

我不明白......是吗?

  SET @balance:=0;
  SELECT date, odds, status,
      @balance:=@balance+IF(status='wrong', 0, odds*100)-100 as balance
  FROM betting
  ORDER BY date;

答案 1 :(得分:0)

问题可能是,由于连接,变量会在较早阶段进行评估。

尝试嵌入主查询,强制mysql最终评估变量:

SET @balance = 0;
 SELECT *, 
    CASE WHEN stats =  'wrong'
    THEN @balance := @balance -100
    ELSE @balance := @balance + ( odds * 100 - 100 ) 
    END as balance
    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