赞
踩
1、简化select的字段,方便管理。
2、可以针对不同用户,对视图进行不同的查看。也就是说可以做权限管理。
3、视图的语法
CREATE VIEW MYTEST AS
select 列车号,车辆编号,车门编号,
没有使用视图之前的一个查询语句:
select 列车号,车辆编号,车门编号, case when 霍尔信号异常 != 0 then '霍尔信号异常' when 电机过流故障 != 0 then '电机过流故障' when 门锁到位开关故障 != 0 then '门锁到位开关故障' when 安全互锁回路异常故障 != 0 then '安全互锁回路异常故障' when 门关到位开关故障 != 0 then '门关到位开关故障' when 门地址编码故障 != 0 then '门地址编码故障' when 开门超时故障 != 0 then '开门超时故障' when 电机过流故障 != 0 then '电机过流故障' when 关门超时故障 != 0 then '关门超时故障' when 蜂鸣器输出口短路故障 != 0 then '蜂鸣器输出口短路故障' when 门FSR故障 != 0 then '门FSR故障' when 门测试开关故障 != 0 then '门测试开关故障' when 门开故障 != 0 then '门开故障' when 门关故障 != 0 then '门关故障' when 内部控制电压故障 != 0 then '内部控制电压故障' when 内部RS485总线通信故障 != 0 then '内部RS485总线通信故障' when 门未经许可离开关锁到位位置故障 != 0 then '门未经许可离开关锁到位位置故障' when 关门防挤压故障 != 0 then '关门防挤压故障' when 开门防挤压故障 != 0 then '开门防挤压故障' when 门故障 != 0 then '门故障' when 门驱动电机电路断路故障 != 0 then '门驱动电机电路断路故障' when 门位置传感器故障 != 0 then '门位置传感器故障' when 数据总线通信故障 != 0 then '数据总线通信故障' when 电磁铁故障 != 0 then '电磁铁故障' when 电钥匙开关故障 != 0 then '电钥匙开关故障' end '故障名称', case when 霍尔信号异常 != 0 then 'Y004001' when 电机过流故障 != 0 then 'Y004002' when 门锁到位开关故障 != 0 then 'Y005001' when 安全互锁回路异常故障 != 0 then 'Y001001' when 门关到位开关故障 != 0 then 'Y006001' when 门地址编码故障 != 0 then 'Y003001' when 开门超时故障 != 0 then 'Y006002' when 电机过流故障 != 0 then '123' when 关门超时故障 != 0 then 'Y006003' when 蜂鸣器输出口短路故障 != 0 then 'Y003002' when 门FSR故障 != 0 then 'Y006004' when 门测试开关故障 != 0 then 'Y006005' when 门开故障 != 0 then 'Y006006' when 门关故障 != 0 then 'Y006007' when 内部控制电压故障 != 0 then 'Y003003' when 内部RS485总线通信故障 != 0 then 'Y002001' when 门未经许可离开关锁到位位置故障 != 0 then 'Y006008' when 关门防挤压故障 != 0 then 'Y006009' when 开门防挤压故障 != 0 then 'Y006010' when 门故障 != 0 then 'Y006011' when 门驱动电机电路断路故障 != 0 then 'Y004003' when 门位置传感器故障 != 0 then 'Y006012' when 数据总线通信故障 != 0 then 'Y002001' when 电磁铁故障 != 0 then 'Y007001' when 电钥匙开关故障 != 0 then 'Y007002' end '故障代码' from 2故障预警登记表 WHERE 霍尔信号异常 != 0 or 电机过流故障 != 0 or 门锁到位开关故障 != 0 or 安全互锁回路异常故障 != 0 or 门关到位开关故障 != 0 or 门地址编码故障 != 0 or 开门超时故障 != 0 or 电机过流故障 != 0 or 关门超时故障 != 0 or 蜂鸣器输出口短路故障 != 0 or 门FSR故障 != 0 or 门测试开关故障 != 0 or 门开故障 != 0 or 门关故障 != 0 or 内部控制电压故障 != 0 or 内部RS485总线通信故障 != 0 or 门未经许可离开关锁到位位置故障 != 0 or 关门防挤压故障 != 0 or 开门防挤压故障 != 0 or 门故障 != 0 or 门驱动电机电路断路故障 != 0 or 门位置传感器故障 != 0 or 数据总线通信故障 != 0 or 电磁铁故障 != 0 or 电钥匙开关故障 != 0 ORDER BY 故障名称
查询结果如下所示:
创建视图:
CREATE VIEW MYTEST AS select 列车号,车辆编号,车门编号, case when 霍尔信号异常 != 0 then '霍尔信号异常' when 电机过流故障 != 0 then '电机过流故障' when 门锁到位开关故障 != 0 then '门锁到位开关故障' when 安全互锁回路异常故障 != 0 then '安全互锁回路异常故障' when 门关到位开关故障 != 0 then '门关到位开关故障' when 门地址编码故障 != 0 then '门地址编码故障' when 开门超时故障 != 0 then '开门超时故障' when 电机过流故障 != 0 then '电机过流故障' when 关门超时故障 != 0 then '关门超时故障' when 蜂鸣器输出口短路故障 != 0 then '蜂鸣器输出口短路故障' when 门FSR故障 != 0 then '门FSR故障' when 门测试开关故障 != 0 then '门测试开关故障' when 门开故障 != 0 then '门开故障' when 门关故障 != 0 then '门关故障' when 内部控制电压故障 != 0 then '内部控制电压故障' when 内部RS485总线通信故障 != 0 then '内部RS485总线通信故障' when 门未经许可离开关锁到位位置故障 != 0 then '门未经许可离开关锁到位位置故障' when 关门防挤压故障 != 0 then '关门防挤压故障' when 开门防挤压故障 != 0 then '开门防挤压故障' when 门故障 != 0 then '门故障' when 门驱动电机电路断路故障 != 0 then '门驱动电机电路断路故障' when 门位置传感器故障 != 0 then '门位置传感器故障' when 数据总线通信故障 != 0 then '数据总线通信故障' when 电磁铁故障 != 0 then '电磁铁故障' when 电钥匙开关故障 != 0 then '电钥匙开关故障' end '故障名称', case when 霍尔信号异常 != 0 then 'Y004001' when 电机过流故障 != 0 then 'Y004002' when 门锁到位开关故障 != 0 then 'Y005001' when 安全互锁回路异常故障 != 0 then 'Y001001' when 门关到位开关故障 != 0 then 'Y006001' when 门地址编码故障 != 0 then 'Y003001' when 开门超时故障 != 0 then 'Y006002' when 电机过流故障 != 0 then '123' when 关门超时故障 != 0 then 'Y006003' when 蜂鸣器输出口短路故障 != 0 then 'Y003002' when 门FSR故障 != 0 then 'Y006004' when 门测试开关故障 != 0 then 'Y006005' when 门开故障 != 0 then 'Y006006' when 门关故障 != 0 then 'Y006007' when 内部控制电压故障 != 0 then 'Y003003' when 内部RS485总线通信故障 != 0 then 'Y002001' when 门未经许可离开关锁到位位置故障 != 0 then 'Y006008' when 关门防挤压故障 != 0 then 'Y006009' when 开门防挤压故障 != 0 then 'Y006010' when 门故障 != 0 then 'Y006011' when 门驱动电机电路断路故障 != 0 then 'Y004003' when 门位置传感器故障 != 0 then 'Y006012' when 数据总线通信故障 != 0 then 'Y002001' when 电磁铁故障 != 0 then 'Y007001' when 电钥匙开关故障 != 0 then 'Y007002' end '故障代码' from 2故障预警登记表 WHERE 霍尔信号异常 != 0 or 电机过流故障 != 0 or 门锁到位开关故障 != 0 or 安全互锁回路异常故障 != 0 or 门关到位开关故障 != 0 or 门地址编码故障 != 0 or 开门超时故障 != 0 or 电机过流故障 != 0 or 关门超时故障 != 0 or 蜂鸣器输出口短路故障 != 0 or 门FSR故障 != 0 or 门测试开关故障 != 0 or 门开故障 != 0 or 门关故障 != 0 or 内部控制电压故障 != 0 or 内部RS485总线通信故障 != 0 or 门未经许可离开关锁到位位置故障 != 0 or 关门防挤压故障 != 0 or 开门防挤压故障 != 0 or 门故障 != 0 or 门驱动电机电路断路故障 != 0 or 门位置传感器故障 != 0 or 数据总线通信故障 != 0 or 电磁铁故障 != 0 or 电钥匙开关故障 != 0 ORDER BY 故障名称
使用创建视图之后的查询语句:
2022.9.19视图的使用
SELECT * FROM MYTEST;
实验结果:
结论:效果一致 ,但是大大减少的查询语句的使用!!
2、在Navicat中创建好视图之后,可以在QT开发软件中直接使用创建好的视图。
这里通过QT操作视图和之前Navicat操作视图的访问结果是完全一致的。
DESCRIBE 可以用来查看视图,语法如下:
DESCRIBE 视图名
DESCRIBE MYTEST
CREATE OR REPLACE VIEW 视图名 AS SELECT [...] FROM [...];
DROP VIEW [IF EXISTS] view_name ,[view_name] ;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。