您好我正在尝试查询当前的余额,但它似乎在最后显示了正确的平衡,但整体不正确。所以基本上计算是
IF状态等于'错误'
balance = balance - 100
ELSE
余额=余额+(赔率* 100)
END
下面你看我当前查询的输出:
Odds Status Date balance
1.70 | wrong | 2015-02-08 13:22:17 | 205
3.05 | correct | 2015-02-08 20:27:44 | 305
2.20 | correct | 2015-02-09 15:45:27 | 425
根据我的计算,余额应为
-100
205
425
那为什么不显示这个?
SET @balance = 0;
SELECT odds.meta_value AS odds, stat.meta_value AS stats, posts.post_date as postdate,
CASE WHEN stat.meta_value = 'wrong'
THEN @balance := @balance -100
ELSE @balance := @balance + (odds.meta_value * 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
答案 0 :(得分:0)
我还没有仔细查看过您的代码,但在使用变量之前请考虑这一点(来自MySQL文档):
作为一般规则,除了在SET语句中,您不应该为用户变量赋值并在同一语句中读取值。例如,要增加变量,这没关系:
SET @a = @a + 1;
对于其他语句,例如SELECT,您可能会得到您期望的结果,但这不能保证。在下面的语句中,您可能会认为MySQL将首先评估@a,然后再进行一次分配:
SELECT @ a,@ a:= @ a + 1,...;
但是,涉及用户变量的表达式的评估顺序是未定义的。
您可以在此处找到更多信息:http://dev.mysql.com/doc/refman/5.7/en/optimizing-myisam-bulk-data-loading.html