1

我无法IN使用单引号选择带有子句的值。列pidvarchar(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')

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

4

1 回答 1

1

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

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

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

SQL Fiddle 在这里。代码是:

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')
于 2013-05-11T20:26:52.713 回答