IN运算符未找到匹配项

时间:2013-05-11 18:12:09

标签: sql sql-server sql-server-2012 sql-server-2012-express

我无法使用单引号选择IN子句的值。 pid列为varchar(50)

select * from t_tra_main where pid in (200000002,300000394,200000004,
    200000001,300000378,300000393,300000379,200000003)

select * from t_tra_main where pid in ('200000002','300000394','200000004',
    '200000001','300000378','300000393','300000379','200000003')

第一个查询返回数据,但第二个查询不返回。

1 个答案:

答案 0 :(得分:1)

进行一些实验,我认为表中的值在开头有空格或零。

对于OP,您可以通过运行以下查询来测试:

select *
from t_tra_main
where left(pid) in (' ', '0');

SQL小提琴是here。代码是:

create table t_tra_main (
  id int identity(1, 1),
  pid varchar(50)
);

insert into t_tra_main(pid)
    select '200000002' union all
    select '0300000394' union all
    select '0200000004' union all
    select ' 200000001' union all
    select ' 300000378' union all
    select '300000393 ' union all
    select '300000379 ' union all
    select '200000003';

select * from t_tra_main where pid in (200000002,300000394,200000004,
    200000001,300000378,300000393,300000379,200000003)

select * from t_tra_main where pid in ('200000002','300000394','200000004',
    '200000001','300000378','300000393','300000379','200000003')