【SQL】查询很多同一列的 不用AND 怎么精简

来源:百度知道 编辑:UC知道 时间:2024/06/04 04:07:53

select distinct(ip_address) ,computer,lastcheckintime,productversion from inventory where
(
computer not like 'ABD%'
and computer not like 'BCCC%'
and computer not like 'CCCC%'
and computer not like 'DDD%'
and computer not like 'KPI%'
and computer not like 'ADA%'
...........................
...........................
)
and productversion <> ''
order by computer ASC

中间那些怎么精简,用group?具体怎么写
.... 这个确实难处理啊...BOSS叫把所有命名不规范的终端找出来,命名规则为部门中文名+使用者姓名,几万条记录,我只有把部门名筛出来了... 部门名也几十个...还有更好的办法么 = =! 求教
问题是就算是中文名也有可能不规范啊,还有乱七八糟的英文,但是有的部门又是英文,我OOXX。。。

建一个表,叫作字典吧,把您能想起来的添加到表里,表的记录内容可以无限制地添加,如:
create table mydict(mywords varchar(100))
内容为
'ABD',
'BCCC',
'CCCC'
'DDD'
'KPI'
'ADA'
.
.
.
等。
然后就可以将您的语句改为:
select distinct(ip_address) ,computer,lastcheckintime,productversion from inventory where
(
not exists(select * from mydict where computer like mywords+'%')
)
and productversion <> ''
order by computer ASC

不是group,group是分组用的,你要分组么?我觉得正则表达式应该可以帮助你(前提是那些要匹配的字符要有规律)比如:
select * from t where s1 regexp('[0-9]');
这句就会筛选所有s1字段的字符串里所有字符都在0123456789之内的【0-9】就是一个正则表达,详细参见http://baike.baidu.com/view/1427403.htm
再看你的,类似匹配前面首字母固定s,紧接的可以是ta也可以是er,后面随意,象下面就可以了
select * from keyword where keywords regexp('^s(er|ta)');
实在没规律也可以
('^(abd|BCC