PostgreSQL:使用where子句来改变选择值

时间:2016-03-24 01:44:26

标签: postgresql

我想根据select子句更改where值。是否可能以下任何方式?

SELECT b
FROM table
WHERE 
    IF date = '2016-03-24'
        b = 1
    ELSE IF date > date '2016-03-24' - 7 AND date < '2016-03-24'
        b = 2
    ELSE
        b = 1
ORDER BY date
LIMIT 1

1 个答案:

答案 0 :(得分:1)

使用CASE WHEN

SELECT CASE WHEN date = '2016-03-24' THEN 1
            WHEN date > '2016-03-17' AND date < '2016-03-24' THEN 2
            ELSE 1
       END AS b
FROM table
ORDER BY b
LIMIT 1