我如何使用select为表填充具有特定固定值的列

时间:2019-05-06 21:33:56

标签: psql

我想选择一些数据,但是该数据具有一列包含NULL值的列。我希望它在不更改数据库的情况下将其从null更改为特定的固定值

V_fruits

number | fruit     | three
1      | apple     | <null>
2      | pinapple  | <null>
3      | grape     | <null>
4      | lemon     | <null>

我想要使用

Select "number","fruit",case when "three" is null then three='ofcourse'  from  V_fruits

我想要一些指导,请Psql

预期 V_fruits

number fruit      three
1      apple      Of course
2      pinapple   Of course
3      grape      Of course
4      lemon      Of course

已获得 V_fruits

number  fruit    three
1       apple    false
2       pinapple false
3       grape    false
4       lemon    false

1 个答案:

答案 0 :(得分:0)

相反:

Select "number","fruit",case when "three" is null then 'ofcourse' END  from  V_fruits

这里的区别在于three='ofcourse'被求值并返回false,因为three为空,因此不能为'ofcourse'

您可以选择使用:

SELECT "number", "fruit", COALESCE(three, 'ofcourse') FROM v_fruits;
相关问题