有A表
a b c三个字段B表
a e f所要结果是根据A表的a判断B表里是否存在a这条数据,如果存在则状态为1不存在则为0select a.*,status[判断的状态] from a join b 
具体语句不知道怎么写,还希望各位帮助下。

解决方案 »

  1.   


    select a.*,1 as status from a join b on a.a=b.a
    union all
    select a.*,0 as status from a left join b on a.a=b.a where a.a is nullselect a.*,1 as status from a where exists(select 1 from b where b.a=a.a)
    union all
    select a.*,0 as status from a where not exists(select 1 from b where b.a=a.a)
      

  2.   


    select a.*,status = (case when b.a is not null then 1 else 0 end)
    from a left join b on a.a = b.a
      

  3.   

    select A.*,case (select count(*) from A tb where tb.a=A.a) when >0 then 1 else 0 end as status from A
      

  4.   


    select a.*,status = (case when exists(select 1 from b where a.a = b.a) then 1 else 0 end)
    from a
      

  5.   


    select x1.*,case when x2.a is null then '0' else '1' end status
     from A x1 left join B x2 on x1.a=x2.a
      

  6.   

    select A.*,(case when a in (select a from B) then 1 else 0 end) [state] from A