MHB2011
MHB2011

Reputation: 463

mysql query with if and else statement

I need mysql query that will :

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

but i dont know whats the best solution to write query like this.

Upvotes: 1

Views: 100

Answers (2)

juergen d
juergen d

Reputation: 204746

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

Upvotes: 3

Gordon Linoff
Gordon Linoff

Reputation: 1269443

I think you can do what you want with a case statement:

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;

Upvotes: 2

Related Questions