从一个个长度为7位的字符串列中查询满足以下条件的记录:第1位 = 2 or * ,第2位 = 3 or * ,第3位 = 8 or * ,第4位 = 5 or * ,第5位 = 9 or * ,第6位 = 7 or * ,第7位 = 4 or *
怎么能用简单方法实现,即不通过一个个字符比较来实现。
这个问题第1个回答:
SQL code
select col from tb where substring(col,1,1) in(1,'*') and substring(col,2,1) in(3,'*') and
substring(col,3,1) in(8,'*') and substring(col,4,1) in(5,'*') and substring(col,5,1) in(9,'*')
and substring(col,6,1) in(7,'*') and substring(col,7,1) in(4,'*')
这个问题第2个回答:
把所有可能存一个表中? 这个问题第3个回答:
不是很明白 LZ的意思。。。
这个问题第4个回答:
SQL code
declare @t table(col varchar(10))
insert @t select '2**5974'
insert @t select '*3**9**'
insert @t select '2*2597s'
select col from @t where substring(col,1,1) in(2,'*') and substring(col,2,1) in(3,'*') and
substring(col,3,1) in(8,'*') and substring(col,4,1) in(5,'*') and substring(col,5,1) in(9,'*')
and substring(col,6,1) in(7,'*') and substring(col,7,1) in(4,'*')
/*
col
----------
2**5974
*3**9**
*/
这个问题第5个回答:
kankan
这个问题第6个回答:
SQL code
declare @t table(col varchar(10))
insert @t select '2**5974'
insert @t select '*3**9**'
insert @t select '2*2597s'
select col
from @t
where col like '[2*][3*][8*][5*][9*][7*][4*]'
/*
col
----------
2**5974
*3**9**
(2 行受影响)
*/