如果两列为1,则创建新列

时间:2014-06-18 04:01:59

标签: mysql sql phpmyadmin

我需要一个我的sql语句,它选择与此类似的东西

SELECT present, wholeday, attendance

presentwholeday被给予,而出席率则由现在和出席者组合生成

if present == 1 and wholeday == 1 then attendance = 1

if present == 1 and wholeday == 0 then attendance = .5

if present == 0 and wholeday == 0 then attendance = 0

1 个答案:

答案 0 :(得分:0)

您的查询将是:

SELECT PRESENT, WHOLEDAY,
       CASE
           WHEN  (PRESENT = 1 AND WHOLEDAY = 1) THEN 1
           WHEN  (PRESENT = 1 AND WHOLEDAY = 0) THEN 0.5
           ELSE  0
       END as ATTENDANCE 
FROM MY_TABLE

Case语法是:

CASE
    WHEN condition_1 THEN commands
    WHEN condition_2 THEN commands
    ...
    ELSE commands
END CASE;