id  productid  count   表名a
1      1            2
2      2            3id  number  g_id    a_id  表名b
1  gb2312123  2,1  SELECT * FROM a WHERE id in(select a_id from b)
这样写怎么只能把 a表 id为2 的数据读出来SELECT * FROM a WHERE id in(1,2)
这样写都可以读出该  怎么修改我的sql语句

解决方案 »

  1.   

    id productid count 表名a
    1 1 2
    2 2 3id  number      a_id  表名b
    1   gb2312123    2,1 
    写错了 是这个格式
      

  2.   

    drop table a,b;
    create table a(id int,productid int,count int);
    insert a
    select 1,1,2 union all
    select 2,2,3;create table b(id int,number varchar(9),a_id varchar(3));
    insert b
    select 1,'gb2312123','2,1';
    select a.* from a,b where FIND_IN_SET(a.id,b.a_id)/*1 1 2
    2 2 3
    */
      

  3.   

    declare @aid nvarchar(50)
    declare @sql nvarchar(500)
    set @aid=(select a_id from b where id=1)
    set @sql='select * from a where id in('+@aid+')'
    exec(@sql)