表 TAB_A  字段  id,name,type
表 TAB_B  字段  id,aId,status     其中aId关联TAB_A的id1、select a.id,a.name,a.type,b.id from TAB_A ,TAB_B b where a.type=2 and a.id=b.aId and b.status!=1
2、 select a.id,a.name,a.type,b.id from TAB_A a where a.type=2 and a.id not in(select b.aId from TAB_B b)现在要将这两条语句合并,就是将这两条语句查询出的数据用一条语句直接一起查询出来

解决方案 »

  1.   

    select a.id,a.name,a.type,b.id from TAB_A ,TAB_B b where a.type=2 and a.id=b.aId and b.status!=1
    union all
    select a.id,a.name,a.type,b.id from TAB_A a where a.type=2 and a.id not in(select b.aId from TAB_B b)
      

  2.   


    select a.id,a.name,a.type,b.id from TAB_A ,TAB_B b where a.type=2 and a.id=b.aId and b.status!=1
    union all
    select a.id,a.name,a.type,b.id from TAB_A a where a.type=2 and a.id not in(select b.aId from TAB_B b)
      

  3.   

    第二个语句本身就有错吧
    ,b.id 怎么出来的?
      

  4.   


    select a.id,a.name,a.type,b.id 
    from Tab_A a,Tab_B b
    where a.type = 2
    and 1 =  case when b.status <> 1 then 1 else 0 end
      

  5.   

    第二条语句有问题,合并俩个结果集使用union或union all,条件俩个输入必须有相同的列数,而且相应列必须具有相同数据类型,或者可以隐式转换,结果列由第一个输入的列决定
      

  6.   

    用union all可以实现,不过用4楼的方法我没查出数据
    case 不会用。。
    我的实际SQL是
    select a.id,a.name,a.org_id
    from pi_t_personnel a,pi_t_apply_join_party b
    where a.personnel_status=2 and a.org_id=101
    and 1 = case when b.personnel_id=a.id and b.join_degree<>1 then  1 else 0 end
      

  7.   

    7 楼的SQL你仔细检查一下,看看有没有错,我这里没经过实际的数据测试
    select a.id,a.name,a.org_id
    from pi_t_personnel a,pi_t_apply_join_party b
    where a.personnel_status=2 and a.org_id=101
    and 1 = case when b.personnel_id=a.id and b.join_degree<>1 then 1 else 0 end
      

  8.   

    是不是and a.org_id=101这个条件限制掉了?
      

  9.   

    8楼的语句我用过,不过只查出了b.personnel_id=a.id and b.join_degree<>1 一种情况,a.id not in(select b.aId from TAB_B b这种情况的数据未查出,
    如果
    select a.id,a.name,a.org_id
    from pi_t_personnel a,pi_t_apply_join_party b
    where a.personnel_status=2 and a.org_id=101
    and 1 = case 
    when b.personnel_id=a.id and b.join_degree<>1 then 1 
    when a.id not in(select b.aId from TAB_B b then 1
    else 0 end
    又会出现数据重复
      

  10.   

    union all会把重复的也列出来,用union就能只列一个重复值