mySQL查询返回错误的结果

时间:2015-02-12 20:08:40

标签: mysql

我正在尝试从赔率,状态和赌注中计算当前余额。

状态='错误'或'正确'

赔率=例如1.70

赌注=固定100

所以计算就像是

If status = wrong THEN
balance = balance - 100
ELSE
balance = balance + (odds * 100 - 100)
END

我已尝试在我的查询中实现此功能,但它似乎有效,但它在余额列中显示错误的结果。

我的查询:

    SET @balance = 0;
     SELECT odds.meta_value AS odds, stat.meta_value AS stats, 
                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.id

这里的输出是

1.70    wrong   105
3.05    correct 205
2.20    correct 225
1.33    correct 258
1.35    correct 293
1.38    correct 231
2.32    wrong   193

因为平衡计算错误,我决定尝试制作一个SQLFiddle,用完全相同的顺序,数据和平衡sql计算来测试数据,你会看到这个计算是正确的。我的查询没有像在sqlfiddle中那样显示这个结果的原因是什么?

http://sqlfiddle.com/#!2/ea13c/1

0 个答案:

没有答案