2015-02-23 58 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