【问题标题】:If two columns are 1 then make new column如果两列是 1 则创建新列
【发布时间】:2014-08-08 05:39:08
【问题描述】:

我需要一个我的 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

【问题讨论】:

  • 所以您希望我们帮助您而不需要您的任何努力?
  • 您已经掌握了逻辑,是什么阻碍了您编写 SQL 查询?
  • 既然没有 case for present=0, wholeday=1, (present + wholeday)/2.0 应该怎么做?

标签: mysql sql phpmyadmin


【解决方案1】:

您的查询将是:

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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-27
    • 2020-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多