+ -
当前位置:首页 → 问答吧 → 求个 sql 语句 数据 简单点的。。

求个 sql 语句 数据 简单点的。。

时间:2011-11-16

来源:互联网

在 数据库 有个这样的 数据 18,15,16



怎么查询 包含 18 和 15 的这2个 数字 的 数据


我开始用 like 18%15 这样查询 是可以的

但发觉 15%18 就不行。。。

怎么查呢??

作者: qunqingkai3   发布时间: 2011-11-16

SQL code

where charindex(',15,',','+col+',') > 0 and charindex(',18,',','+col+',') > 0

作者: AcHerat   发布时间: 2011-11-16

引用楼主 qunqingkai3 的回复:
在 数据库 有个这样的 数据 18,15,16



怎么查询 包含 18 和 15 的这2个 数字 的 数据


我开始用 like 18%15 这样查询 是可以的

但发觉 15%18 就不行。。。

怎么查呢??

select * from tb 
where ','+col+',' like '%,' + '18' + ',%' and ','+col+',' like '%,' + '15' + ',%'

作者: dawugui   发布时间: 2011-11-16

用 col=18 or col=15

作者: qianjin036a   发布时间: 2011-11-16

SQL code

select * from tb where charindex(',18,',col1)>0 and charindex(',15,',col1)>0

作者: pengxuan   发布时间: 2011-11-16

select * from tb  
where charindex(',' + '18' + ',' , ','+col+',') > 0
and charindex(',' + '15' + ',' , ','+col+',') > 0

作者: dawugui   发布时间: 2011-11-16

更正一下
SQL code

select * from tb where charindex(',18,',','+col1+',')>0 and charindex(',15,',','+col1+',')>0

作者: pengxuan   发布时间: 2011-11-16

SQL code
col like ',15,' or col like ',18,'

作者: fredrickhu   发布时间: 2011-11-16

SQL code
where ','+ltrim(col)+',' like ',15,' or col like ',18,'

作者: fredrickhu   发布时间: 2011-11-16

SQL code
where ','+ltrim(col)+',' like ',15,' or ','+ltrim(col)+',' like ',18,'

作者: fredrickhu   发布时间: 2011-11-16

不是OR F姐!

作者: AcHerat   发布时间: 2011-11-16

引用 10 楼 acherat 的回复:
不是OR F姐!
哎...晴天的文章你没有看吧?

作者: dawugui   发布时间: 2011-11-16

乌龟想说什么呢?

作者: AcHerat   发布时间: 2011-11-16

楼上几位的都可以...看看大鬼要说什么

作者: abclm   发布时间: 2011-11-16

引用 12 楼 acherat 的回复:
乌龟想说什么呢?
你让晴天帮你解释一下.

作者: dawugui   发布时间: 2011-11-16

SQL code
if object_id('t3') is not null
drop table t3
create table t3 
(id int identity(1,1) primary key,name varchar(20))
go
insert t3
select '18,15,20' union all
select '18,20,20,15' union all
select '18,20' union all
select '15,20' union all
select '20,18,20,15' union all
select '20,15,20,18'
go
select * from t3 where 
name like '%15%' and name like '%18%' 
and (name like '%,15,%' or name like '15,%' or name like '%,15')
and (name like '%,18,%' or name like '18,%' or name like '%,18')
/*
(所影响的行数为 4 行)
id       name
---      -----
1    18,15,20
2    18,20,20,15
5    20,18,20,15
6    20,15,20,18
*/
go 
drop table t3

作者: xiaolinyouni   发布时间: 2011-11-16

引用 15 楼 xiaolinyouni 的回复:

SQL code
if object_id('t3') is not null
drop table t3
create table t3
(id int identity(1,1) primary key,name varchar(20))
go
insert t3
select '18,15,20' union all
select '18,20,20,15' union all
sel……

改为
SQL code
select * from t3 where 
(name like '%,15,%' or name like '15,%' or name like '%,15')
and (name like '%,18,%' or name like '18,%' or name like '%,18')

作者: xiaolinyouni   发布时间: 2011-11-16

热门下载

更多