首页 新闻 会员 周边

sql语句中的匹配

0
[已解决问题] 解决于 2012-03-17 17:59

就是我现在有1,2,3,4这个字符串,数据库中有个字段只是字符串中其中一个(1)(2)(3)(4)怎么能1,2,3,4能够匹配出来1这一条啊,也就是说把like那种效果倒过来啊。like是1%可以匹配,但是现在我想让倒过来,请问怎么弄啊,能给个确切的代码对好我的是数据库2005

问题补充: 我是想用 1,2,3去匹配出来1这个,或者2这个,
rains的主页 rains | 小虾三级 | 园豆:860
提问于:2010-09-07 17:50
< >
分享
最佳答案
0

如果字段只是(1)(2)(3)(4)中的一个,比如(1)可以这样

use Testdb2
go


IF NOT OBJECT_ID('[t_Demo]') IS NULL
DROP TABLE [t_Demo]
GO
CREATE TABLE [t_Demo]
(
[ID] int identity(1,1) primary key not null,
[Cate] Nvarchar(20) null
)
go

INSERT [t_Demo]
SELECT '(1)' union all
SELECT '(2)' union all
SELECT '(3)' union all
SELECT '(4)' union all
SELECT '(2)' union all
SELECT '(1)'
go

Declare @vars nvarchar(100)
set @Vars='1,2,3,5'
Declare @strSql Nvarchar(max)
set @strSql='select * from [t_Demo] where Replace(Replace([Cate],''('',''''),'')'','''') in ('+@Vars+')'
print @strSql
exec (@strSql)

/*
select * from [t_Demo] where Replace(Replace([Cate],'(',''),')','') in (1,2,3,5)

(5 row(s) affected)
ID Cate
1 (1)
2 (2)
3 (3)
5 (2)
6 (1)

 

邀月 | 高人七级 |园豆:25475 | 2010-09-08 09:03
其他回答(1)
0

不用like,用=。自己拼字符串

一滴血 | 园豆:1602 (小虾三级) | 2010-09-07 18:31
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册