我为我的列创建不同的标签,这取决于不同的条件。它看起来像这样:
, CASE
WHEN phone like '%555%' THEN 1
ELSE 0
END AS first_label
, CASE
WHEN phone like '%888%' THEN 1
ELSE 0
END AS second_label
但我也需要在每行的新列中命名这个新标签,比如lable name = 'first_label'
。那么,我如何才能做到这一点,而不复制块再次?我的结果:
SELECT
, phone
, CASE
WHEN phone like '%555%' THEN 1
ELSE 0
END AS first_label
, CASE
WHEN phone like '%888%' THEN 1
ELSE 0
END AS second_label
, 'lable_name' as lable_name
FROM mytable
预期产出:
| 电话|第一标号|第二标号|标签名|
| - -----|- -----|- -----|- -----|
| 555 75 71 1| 1| 0|第一标号|
| 888 94 18 3| 0| 1|第二标号|
1条答案
按热度按时间lf3rwulv1#
为了避免再次重复表达式,只需从查询结果中选择即可。例如:
演示:https://dbfiddle.uk/zwDO1N1Q