首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求一条SQL查询语句的写法,该如何解决

2012-04-05 
求一条SQL查询语句的写法字符串S为 1,5,8某表A内容如下字段:a1 a2 a35 9 71 5 42 6 1要得知字符串S 在表

求一条SQL查询语句的写法
字符串S为 "1,5,8"


某表A内容如下
字段:a1 a2 a3
  5 9 7
  1 5 4
  2 6 1



要得知字符串S 在表A中存在几个值,比如本例实际就存在2个值:5和1(8不存在)
查询语句要得到的结果就是2 最好用一条语句完成!

请帮忙,第一个正解就给全分!

[解决办法]

SQL code
declare @s varchar(10) set @s='1,5,8'select * from 你的表名 where charindex(','+ltrim(a1)+',',','+@s+',')=0
[解决办法]
SQL code
goif OBJECT_ID('#tb')is not nulldrop table #tbgocreate table #tb(a1 int,a2 int,a3 int)goinsert #tbselect 5,9,7 union allselect 1,5,4 union allselect 2,6,1declare @str varchar(max)set @str='1,5,8'select COUNT(a) as times from(select * from(select a1 as a from #tbunionselect a2 from #tbunionselect a3 from #tb) a where CHARINDEX(ltrim(a),@str)>0)b/*times2*/--查询出现的数字declare @str varchar(max)set @str='1,5,8'select * from(select a1 as a from #tbunionselect a2 from #tbunionselect a3 from #tb) a where CHARINDEX(ltrim(a),@str)>0/*a15*/ 

热点排行