select id,name,pass from A where type = 1
union
if((select id,name,pass from A where type =1) <> 0)
  begin
    select id,name,pass from B
  end
else
  begin
    select id,name,pass from C
  end像这种类型的表查询,分别独立查询时是可以的,但是一用union合并查询就不行了。
假设A、B、C三表结构完全不同,但是有部分字段名都是相同的。

解决方案 »

  1.   

    if exists (select 1 from A where type=1)
    begin
    select id,name,pass from A where type = 1
    union all
    select id,name,pass from B
    end
    else
    begin
    select id,name,pass from C
    end
      

  2.   


    if((select id,name,pass from A where type =1) <> 0)
      begin
        select id,name,pass from A where type = 1
        union
        select id,name,pass from B
      end
    else
      begin
        select id,name,pass from A where type = 1
        union
        select id,name,pass from C
      end
      

  3.   

    if((select id,name,pass from A where type =1) <> 0)
      begin
        select ltrim(id),ltrim(name),ltrim(pass) from A where type = 1
        union
        select ltrim(id),ltrim(name),ltrim(pass) from B
      end
    else
      begin
        select ltrim(id),ltrim(name),ltrim(pass) from A where type = 1
        union
        select ltrim(id),ltrim(name),ltrim(pass) from C
      end
      

  4.   

    (select id,name,pass from A where type =1) <> 0
    ——这是啥玩意啊???
    严重的语法错误,居然还有人复制、粘贴来用?!
    没有实事求是的精神。