MySQL案例成为新专栏

时间:2018-09-23 14:13:53

标签: mysql sql case

如果我使用MySQL case语句,它将创建一个新列,但不会替换该列。

是否可以替换列而不是新列?

示例:

SELECT *, 
       CASE WHEN housenumber_addition IS NULL THEN 'ABC' 
       ELSE housenumber_addition END AS housenumber_addition 
FROM customer

2 个答案:

答案 0 :(得分:0)

您可以有效地两次选择该列,就像您可以多次选择任何一列一样:

select housenumber_addition, housenumber_addition, housenumber_addition 
from customer

通常,您应该分别选择每一列,并用提供的代码替换housenumber_additionSELECT * is considered harmful anyways.

答案 1 :(得分:0)

只需明确选择要显示在结果集中的列,就您而言,排除 housenumber_addition列即可:

SELECT
    col1,     -- some column, not housenumber_addition
    col2,     -- some other column, also not housenumber_addition
    ...,
    CASE WHEN housenumber_addition IS NULL
         THEN 'ABC' 
         ELSE housenumber_addition END AS housenumber_addition
FROM customer;

请注意,您的CASE表达式用途不大,因为您可能只使用过COALESCE

COALESCE(housenumber_addition, 'ABC')