日期:2014-05-18  浏览次数:20603 次

SQL简单查询语句问题!
例子 表:plat_uidlist 


Uid Areaid State
2335435 8 0
2335436 8 1
2335437 2 2
2335438 8 3
2335439 3 0
2335440 8 0
6677889 8 0
1234578 4 0
4980010 8 0
4980099 8 0



我要写一条语句 以下是要求

号码筛选规则
1、 UID中任意位置出现"888"、"666"、"999"的号码预留。
2、 UID中任意位置出现"AAAA"的号码预留。
3、 UID后四位号码出现以下排列的预留:"ABCD"、"AABB"、"ABAB"、"AAAB"、"ABBB"。
4、 4980000 - 4989999 段内UID预留
5 areaid=8 state<>3



以下是我写的语句 大家帮参考下 是否合理 我求最加优化语句 谢谢 

select * from dbo.PLAT_UIDList
where state<>'3' and areaid='8' and
(uid between '4980000' and '4989999' and 
uid like '%888%' or uid like '%666%' or uid like '%999%')  
or uid like '%AAAA%'  
or right(uid,4) in ('ABCD','AABB','ABAB','AAAB','ABBB') 

我基础不好 我就迷糊在括号上拉!呵呵 大家提下意见 这么写有毛病不 是否合理 



------解决方案--------------------
SQL code
create table plat_uidlist(Uid varchar(10) , Areaid int , State int)
insert into plat_uidlist values('2335435' ,8 ,0 )
insert into plat_uidlist values('2335436' ,8 ,1 )
insert into plat_uidlist values('2335437' ,2 ,2 )
insert into plat_uidlist values('2335438' ,8 ,3 )
insert into plat_uidlist values('2335439' ,3 ,0 )
insert into plat_uidlist values('2335440' ,8 ,0 )
insert into plat_uidlist values('6677889' ,8 ,0 )
insert into plat_uidlist values('1234578' ,4 ,0 )
insert into plat_uidlist values('4980010' ,8 ,0 )
insert into plat_uidlist values('4980099' ,8 ,0 )
go

--符合条件的
select * from plat_uidlist where
charindex('888' , Uid) > 0 or charindex('666' , Uid) > 0 or charindex('999' , Uid) > 0 or
charindex('AAAA' , Uid) > 0 or
right(Uid,4) = 'ABCD' or right(Uid,4) = 'AABB' or right(Uid,4) = 'ABAB' or right(Uid,4) = 'AAAB' or right(Uid,4) = 'ABBB' or
Uid between '4980000' and '4989999' or
(areaid = 8 and state <> 3)
/*
Uid        Areaid      State       
---------- ----------- ----------- 
2335435    8           0
2335436    8           1
2335440    8           0
6677889    8           0
4980010    8           0
4980099    8           0

(所影响的行数为 6 行)
*/

--不符合条件的
select * from plat_uidlist where Uid not in 
(
  select Uid from plat_uidlist where
  charindex('888' , Uid) > 0 or charindex('666' , Uid) > 0 or charindex('999' , Uid) > 0 or
  charindex('AAAA' , Uid) > 0 or
  right(Uid,4) = 'ABCD' or right(Uid,4) = 'AABB' or right(Uid,4) = 'ABAB' or right(Uid,4) = 'AAAB' or right(Uid,4) = 'ABBB' or
  Uid between '4980000' and '4989999' or
  (areaid = 8 and state <> 3)
)
/*
Uid        Areaid      State       
---------- ----------- ----------- 
2335437    2           2
2335438    8           3
2335439    3           0
1234578    4           0

(所影响的行数为 4 行)
*/

--drop table plat_uidlist

------解决方案--------------------
SQL code
create table plat_uidlist(Uid varchar(10) , Areaid int , State int) 
insert into plat_uidlist values('2335435' ,8 ,0 )
insert into plat_uidlist values('2335436' ,8 ,1 )
insert into plat_uidlist values('2335437' ,2 ,2 )
insert into plat_uidlist values('2335438' ,8 ,3 )
insert into plat_uidlist values('2335439' ,3 ,0 )
insert into plat_uidlist values('2335440' ,8 ,0 )
insert into plat_uidlist values('6677889' ,8 ,0 )
insert into plat_uidlist values('1234578' ,4 ,0 )
insert into plat_uidlist values('4980010' ,8 ,0 )
insert into plat_uidlist values('4980099' ,8 ,0 )


select * from plat_uidlist
where
charindex('888',uid)>0 or ch