case语句替换where子句

时间:2018-02-16 13:53:34

标签: sql

我有以下查询:

IndexedSeq[X]

我想重写查询,以便它包含SELECT IMALAC.DANOCENBROJ, '1' as REZULTAT FROM IMALAC LEFT JOIN RACUNIL ON IMALAC.ID = IMALACID WHERE RACUNIL.STATUS = 1 AND ( RACUNIL.VIDSMETKA = 1 OR RACUNIL.VIDSMETKA = 5 OR RACUNIL.VIDSMETKA = 7 OR RACUNIL.VIDSMETKA = 8 OR RACUNIL.VIDSMETKA = 9 ) 语句,该语句应该充当case部分的if / else语句

特别是,条件应该是:

  1. 如果where子句后的代码为where,则true = 1
  2. 其他0
  3. 任何?

1 个答案:

答案 0 :(得分:4)

如果我理解正确,你想要这个:

SELECT  IMALAC.DANOCENBROJ, 
        CASE
            WHEN RACUNIL.STATUS = 1 AND RACUNIL.VIDSMETKA IN (1,5,7,8,9)
            THEN 1
            ELSE 0
        END as REZULTAT
FROM IMALAC
LEFT JOIN RACUNIL
ON IMALAC.ID = IMALACID
;