问题:数据表中某一字段(假设为os)的存值是各个主机的操作系统,现在想在页面中归纳操作系统,并根据归纳后的操作系统类别进行搜索,搜索项有“CentOS”、“Ubuntu”、“RedHat”、“SUSE”、“其他”,“所有”,对应的前端设计传值为“CentOS”、“Ubuntu”、“RedHat”、“SUSE”,“others”,“”
数据库数据内容:
mysql> select hostname,os from hosts group by deviceid;
+-----------------------+--------------------------------------------------------+
| hostname | os |
+-----------------------+--------------------------------------------------------+
| redhat6.2 | Red Hat Enterprise Linux Server release 6.2 (Santiago) |
| redhat7.3 | Red Hat Enterprise Linux Server release 7.3 (Maipo) |
| SyslogVM | Cyber Threat Deep Investigation 2.3.1498 |
| centos6.0 | CentOS Linux release 6.0 (Final) |
| localhost.localdomain | CentOS Linux release 7.3.1611 (Core) |
+-----------------------+--------------------------------------------------------+
5 rows in set (0.00 sec)
当下拉框搜索“CentOS”、“Ubuntu”、“RedHat”、“SUSE”时很简单,直接
where INSTR(os,"CentOS"),当搜索“所有”时,可以
where INSTR(os,'')
但搜索“其他”,该怎样解决呢???
if sysos == "others" :terms = "not (INSTR(h.os,'Red Hat') or INSTR(h.os,'CentOS') or INSTR(h.os,'Ubuntu') or INSTR(h.os,'SUSE'))"else :terms = "INSTR(h.os,'{item}')" .format( item =sysos)SQL = "where {term} ".format( term =terms )