【问题标题】:mysql query with if and else statement使用 if 和 else 语句的 mysql 查询
【发布时间】:2014-04-21 01:32:22
【问题描述】:

我需要 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

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

【问题讨论】:

标签: php mysql


【解决方案1】:
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

【讨论】:

    【解决方案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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      • 2023-02-11
      • 1970-01-01
      • 1970-01-01
      • 2017-10-13
      • 1970-01-01
      • 2011-05-28
      相关资源
      最近更新 更多