读书人

请问 为何查询不到数据

发布时间: 2012-01-14 20:02:35 作者: rapoo

请教 为何查询不到数据?

tableA
30
40
41
30,40,41


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


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

[解决办法]
都in了 肯定查不出来喽
[解决办法]
try this,

SQL code
select * from tableAwhere 1 = 1and id like '%30%'
[解决办法]
select * from tableA
where 1 = 1
and id like '30%'


[解决办法]
SQL code
select * from tableAwhere 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 tableAgocreate table tableA( id varchar(20))goinsert into tableAselect '30' union allselect '40' union allselect '41' union allselect '30,40,41'goselect * from tableA where ','+id+',' like '%,30,%'/*(4 行受影响)id--------------------3030,40,41(2 行受影响)*/
[解决办法]
select * from tableA
where id like '30%'

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

读书人网 >SQL Server

热点推荐