2013-06-03 144 views
0
SELECT a.id, a.username, a.email, a.created, b.user_id, 
    SUM((b.price >500) +100) + SUM(b.price) AS gr, SUM(b.price) AS pris, 
    b.created, c.user_id, c.referrer_id, c.created, d.id, d.beginner, d.winner_id 
FROM users a, accounts b, referrals c, product d 
WHERE a.created 
BETWEEN '2013-05-01' 
AND '2013-05-31' 
AND a.id = b.user_id 
AND a.id = c.user_id 
AND d.beginner != '1' 
AND d.winner_id = a.id 
GROUP BY c.referrer_id 
ORDER BY `pris` ASC 
LIMIT 0 , 3 

我已經制定了這個查詢。的問題,我想在這解決的是與查詢內計算

SUM((b.price >500) +100) + SUM(b.price) AS gr 

我希望它可以像計算如果ID的總價格是一個超過500個月然後添加100.我完全不知道怎麼樣我應該在此查詢中計算一個人的總價格,並且如果一個月內的計數超過500,那麼應加上100?

回答

0

哎呦。重新閱讀你的問題。

所以總價格爲一個ID:

sum(b.price) 

測試時總價爲一個id爲> 500,並新增100如果超過500:

sum(b.price) + case when sum(b.price) >500 then 100 else 0 end as gr 
0

在您的查詢中試試這個。

SUM(if(b.price >500,100,0)) 
1

您可以在這裏使用CASE語句

SUM(CASE WHEN b.price >500 THEN b.price+100 ELSE b.price END) as gr