日期:2014-05-18  浏览次数:20395 次

请教 为何查询不到数据?

tableA
30
40
41
30,40,41


select * from tableA
where 1 = 1
and id in (30)


为什么只查处第一条数据呢?第四条数据没出来?

------解决方案--------------------
都in了 肯定查不出来喽
------解决方案--------------------
try this,
SQL code

select * from tableA
where 1 = 1
and id like '%30%'

------解决方案--------------------
select * from tableA
where 1 = 1
and id like '30%'


------解决方案--------------------
SQL code

select * from tableA
where 1 = 1
   and ',30,' like ','+ltrim(id)+','

------解决方案--------------------
in不是用来进行模糊查询的~
------解决方案--------------------
探讨
tableA
30
40
41
30,40,41


select * from tableA
where 1 = 1
and id in (30)


为什么只查处第一条数据呢?第四条数据没出来?

------解决方案--------------------
SQL code
select * from tableA where 1 = 1 and charindex(',30,' ,','+ltrim(id)+',')>0

------解决方案--------------------
in 后面跟的最小单位是一列。。。不是一列中的某个值
------解决方案--------------------
探讨
引用:
tableA
30
40
41
30,40,41


select * from tableA
where 1 = 1
and id in (30)


为什么只查处第一条数据呢?第四条数据没出来?


我这边在SQL 05中没有问题。正常

------解决方案--------------------
in 确定给定的值是否与子查询或列表中的值匹配。
也就是说第四行中的30,40,41 是否与括号中的30匹配,他们不是括号中的子集,所以查不出来
SQL code

if object_id('tableA') is not null
   drop table tableA
go
create table tableA
(
 id varchar(20)
)
go
insert into tableA
select '30' union all
select '40' union all
select '41' union all
select '30,40,41'
go
select * from tableA where ','+id+',' like '%,30,%'
/*

(4 行受影响)
id
--------------------
30
30,40,41

(2 行受影响)
*/

------解决方案--------------------
select * from tableA
where id like '30%'

这样写才行 因为第四个本身就是一个字段形式的