使用if和else语句进行mysql查询

时间:2014-04-21 01:32:22

标签: php mysql

我需要mysql查询:

IF (result=win){SELECT dobitak} else if (result=half_win) {SELECT dobitak/2} else if (half_lost) {SELECT stake/2} else{0};
FROM matches 
WHERE id=$id

但我不知道写这样的查询的最佳解决方案。

2 个答案:

答案 0 :(得分:3)

select case when result ='win' then dobitak
            when result = 'half_win' then dobitak/2
            when result = 'half_lost' then stake/2
            else 0
       end as result
FROM matches 
WHERE id=$id

答案 1 :(得分:2)

我认为你可以用case声明做你想做的事:

select (case when result = win then dobitak
             when result = half_win then dobitak/2
             when half_lost then stake / 2
             else 0
        end)
from matches
where id = $id;