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?

4

3 回答 3

1

您可以在此处使用 CASE 语句

SUM( CASE WHEN b.price >500 THEN b.price+100 ELSE b.price END) as gr
于 2013-06-03T16:07:07.290 回答
0

在您的查询中尝试此操作。

SUM(if(b.price >500,100,0))
于 2013-06-03T16:06:09.633 回答
0

哎呀。只需重新阅读您的问题。

所以一个ID的总价:

sum(b.price)

测试一个 id 的总价是否 > 500,如果超过 500,则添加 100:

sum(b.price) + case when sum(b.price) >500 then 100 else 0 end as gr
于 2013-06-03T16:07:30.980 回答