如何实现 "包含" 查询 ???????????????????????急!!!!!!!!!!!
表 如下, "数值内容" 的每个数字用 "、"号隔开 名称 数值内容(字符型 )
aaa 1、11、110、210
bbb 3、11、210、111
1,我想查询 出 "数值内容" 中 包含 有 1 并且 包含 有 11 的记录 。结果如下 : 名称 数值内容(字符型 )
aaa 1、11、110、210
2,我想查询 出 "数值内容" 中条件: (包含 有 1 并且 包含 有 11) 或 包含 有 210 的记录 。结果如下 : 名称 数值内容(字符型 )
aaa 1、11、110、210
bbb 3、11、210、111
请问这两个 SQL 语句 怎样写????????
表 如下, "数值内容" 的每个数字用 "、"号隔开 名称 数值内容(字符型 )
aaa 1、11、110、210
bbb 3、11、210、111
1,我想查询 出 "数值内容" 中 包含 有 1 并且 包含 有 11 的记录 。结果如下 : 名称 数值内容(字符型 )
aaa 1、11、110、210
2,我想查询 出 "数值内容" 中条件: (包含 有 1 并且 包含 有 11) 或 包含 有 210 的记录 。结果如下 : 名称 数值内容(字符型 )
aaa 1、11、110、210
bbb 3、11、210、111
请问这两个 SQL 语句 怎样写????????
解决方案 »
- 读取另一台服务器上的自定义函数,提示"包含的前缀数目超过了最大值"?不能读取另一台服务器上的自定义函数吗?
- Access 2000 移植到 SQL Server 2000 的问题
- 如何捕捉SQL Server管理器的SQL语句?请教呀!!!!!!
- sql2005为啥不认distince关键字?
- 在线,,,,,,急 ,,,,能否在SQL的自动编号中设定规则,当编号被删除后还可以再次使用此编号呢,或者 怎么弄才可以让第二个编号是
- sql排序问题
- pengdali(大力 V2.0)请进
- 请教一句Select语句怎么写?很简单的!
- 急!!!!请问各位大哥,mapinfo数据库(mis)怎样导出到sql server.
- 寻找简单的存储过程。
- 进入数据库慢的问题?
- 分类汇总问题(请高手帮忙解答
union select * from tb where 数值内容 like '%11%'
union select * from tb where 数值内容 like '%210%'--效率高--或着
select * from tb where 数值内容 like '%1%'or 数值内容 like '%11%' or 数值内容 like '%210%'
效率低
这样会把有 110、11 的记录也查出来!!!!
union select * from tb where 数值内容 like '1、%'
union select * from tb where 数值内容 like '%、1'
union select * from tb where 数值内容 like '%、11、%'
union select * from tb where 数值内容 like '%、11'
union select * from tb where 数值内容 like '11、%'
union select * from tb where 数值内容 like '%、210、%'
union select * from tb where 数值内容 like '%、210'
union select * from tb where 数值内容 like '210、%'
union select * from tb where charindex('、11、','、'+数值内容+'、')>0
union select * from tb where charindex('、210、','、'+数值内容+'、')>0
这样写也可以
union select * from tb where patindex('%、11、%','、'+数值内容+'、')>0
union select * from tb where patindex('%、210、%','、'+数值内容+'、')>0
我就知道这点方法
不要就要写函数判断了
create table tb1(a varchar(100),b varchar(100))insert into tb1 select 'aaa','1、11、110、210'
insert into tb1 select 'bbb','3、11、210、111'--1 SELECT * from tb1
where patindex('%[^0-9]1[^0-9]%',' '+b+' ')>0
and patindex('%[^0-9]11[^0-9]%',' '+b+' ')>0--2SELECT * from tb1
where (patindex('%[^0-9]1[^0-9]%',' '+b+' ')>0
and patindex('%[^0-9]11[^0-9]%',' '+b+' ')>0)
or patindex('%[^0-9]210[^0-9]%',' '+b+' ')>0
drop table tb1
declare @t1 table(sznr varchar(60))insert into @t
select 'aaa' , '1、11、110、210' union
select 'bbb' , '3、11、210、111'
select * from @tdeclare @n int, @n1 int , @n2 int ,@n_con1 int , @n_con2 intdeclare cursor1 scroll cursor for select sznr from @t
open cursor1declare @s varchar(8000)
declare @s1 varchar(8000)
select @s1 = ''
select @s = ''
fetch next from cursor1 into @swhile (@@fetch_status = 0)
begin
select @n1 = charindex('、',@s,1)
print @n1
select @n2 = 1
while @n1 > 0
begin
select @s1 = substring(@s,@n2,@n1-@n2 )
if @s1 = '1'
begin
select @n_con1 = 1
end
if @s1 = '11'
begin
select @n_con2 = 1
end
if(@n_con1 = 1 and @n_con2 = 1 )
begin
insert into @t1
select @s
goto T1
end
select @n2 = @n1 + 1
select @n1 = charindex('、',@s,@n2)
end
T1:
select @n_con1 = 0
select @n_con2 = 0
fetch next from cursor1 into @s
end
close cursor1
deallocate cursor1 select * from @t
where sznr in
(select * from @t1)