select * from tType where iTypeId in (select iTypeId from tProjectTypes where iProjectId=73)这个没问题,我想知道怎么在结果里面增加一个字段,如果在tProjectTypes中有结果这个字段
为1,没有就为0,怎么写这个sql

解决方案 »

  1.   

    --tryselect iTypeId.*,
    结果=case when exists(select 1 from tProjectTypes where iProjectId=73 and iTypeId=tType.iTypeId) then 1 else 0 end
    from tType
      

  2.   

    --try
    select iTypeId.*,
    isnull((select 1 from tProjectTypes where iProjectId=73 and iTypeId=tType.iTypeId),0)
    from tType
      

  3.   

    select *,
      (case when exists(select 1 from syscolumns where object_id('tProjectTypes')=id and 
      name='某字段') 某字段 
    from tType where iTypeId in (select iTypeId from tProjectTypes where iProjectId=73)
      

  4.   

    select *,
      (case when exists(select 1 from syscolumns where object_id('tProjectTypes')=id 
      and name='某字段') then 1 else 0 end ) 某字段 
    from tType where iTypeId in (select iTypeId from tProjectTypes where iProjectId=73)