有四张表电表表Cat1电表表Nb电表表电表类型表其中Cat1电表表和Nb电表表中都有是否在线字段现写个视图用case when根据电表类型判断使用Cat1电表表还是NB电表表的是否在线字段
可以通过以下方式创建视图来实现该要求:
CREATE VIEW meter_status_view AS
SELECT e.meter_id,
e.meter_type,
CASE WHEN m.meter_type = 'Cat1' THEN c.is_online
WHEN m.meter_type = 'Nb' THEN n.is_online
ELSE NULL
END AS is_online
FROM electric_meter_table e
LEFT JOIN cat1_meter_table c ON e.meter_id = c.meter_id
LEFT JOIN nb_meter_table n ON e.meter_id = n.meter_id;
其中,electric_meter_table是电表表,包含meter_id和meter_type字段;cat1_meter_table是Cat1电表表,包含meter_id和is_online字段;nb_meter_table是Nb电表表,包含meter_id和is_online字段。
该视图根据电表类型判断使用Cat1电表表还是Nb电表表的is_online字段,并将结果作为is_online字段返回。如果电表类型不是Cat1或Nb,则is_online字段返回NULL
原文地址: https://www.cveoy.top/t/topic/hLua 著作权归作者所有。请勿转载和采集!