2015-02-23 55 views
0

这MySQL查询给了我这个错误'Unknown column 'winnings' in 'field list'如何检查同一查询中的聚合函数的值?

SELECT 
o.user_id, 
sum(case when o.result = 1 or o.result=2 or o.result = 0 then 1 else 0 end) as tahmins_no, 
sum(case when o.result = 1 then 1 else 0 end) as winnings, 
sum(case when o.result = 2 then 1 else 0 end) as loses, 
sum(case when winnings = 10 then 0.5 else 0 end) as counter 
FROM `odds_tahminler` o 

我知道winningssum()聚合函数的值,但是有什么办法来检查查询中的winnings价值?

+0

我只看到'o'表,你的'u'表在哪里? – Alex 2015-02-23 18:27:36

+0

@Alex你是对的我编辑它 – Basel 2015-02-23 18:35:00

+0

你是什么意思'检查*奖金*值? – Alex 2015-02-23 18:46:44

回答

-1

你可以试试:

SELECT 
o.user_id, 
sum(case when o.result = 1 or o.result=2 or o.result = 0 then 1 else 0 end) as tahmins_no, 
sum(case when o.result = 1 then 1 else 0 end) as winnings, 
sum(case when o.result = 2 then 1 else 0 end) as loses, 
sum(case when winnings = 10 then 0.5 else 0 end) as counter 
FROM `odds_tahminler` o 
GROUP BY o.user_id 
HAVING counter>2 
0

无法使用内选择一个集合列。但是,在计算完所有聚合列后,可以使用子查询来获取计数器值。

如何计算计数器值?如果至少有10个奖金,则我认为该计数器应该是(奖金 - 10)/ 2,否则为0。在这种情况下,你可以用这个查询来获得它

SELECT O.*, 
     GREATEST((O.winnings - 10)/2, 0) as counter    
    FROM 
     (
      SELECT u.username, 
       o.user_id, 
       sum(case when o.result = 1 or o.result=2 or o.result = 0 then 1 else 0 end) as tahmins_no, 
       sum(case when o.result = 1 then 1 else 0 end) as winnings, 
       sum(case when o.result = 2 then 1 else 0 end) as loses 
      FROM `odds_tahminler` o 
     ) as O 
+0

我想你在这个答案中错过了一些东西我想'计数器'为每个继续10'赢得'加1。可能吗 ? 我的意思是我们应该每增加10个连续的奖金,然后让奖金值为零,并且如果我们面对一个失去之前达到10个连续的奖金,我们也应该使奖金为零 – Basel 2015-02-23 19:15:53

+0

好吧,所以你想要如何计数有许多10连胜的条纹。可悲的是你不能用聚合函数来做到这一点。 它必须是一种方法来做到这一点,但我需要时间来思考它,如果我找到解决方案,我会再次回答。 – 2015-02-23 19:44:44