表A
ID name   Address
1  Jerry  GuanZhuo
表B 
ID Name   Address
1  Joy    ShanHai表C
OrderID   Name
1         Jerry
2         Joy因为系统有两个用户表和一个订单表设计的很不好的,但现在要查询出订单表中他们的地址结果为OrderID Name   ID Address
1       Jerry  1  GuanZhuo
2       Joy    1  ShanHai谢谢

解决方案 »

  1.   

    也就是说要表A和表B的数据相结合再和表C用Name来关联起来谢谢
      

  2.   

    select c.* , t.id , t.address
    from c left join (select * from a union all select * from b) t
    on c.name = t.name
      

  3.   

    select a.*,b.id,b.address
    from 表C a,
    (select name,address from 表A union all select name,address from 表B) b
    where a.name=b.name
      

  4.   


    select c.*,
    isnull(a.id,b.id) as id,
    isnull(a.Address,b.Address ) as Address 
    from c left join a on c.name=a.name
    left join b on c.name=b.name
      

  5.   

    select a.*,b.id,b.address 
    from 表C a, 
    (select id,name,address from 表A union all select id,name,address from 表B) b 
    where a.name=b.name 
      

  6.   

    create table A(ID int,name varchar(10),Address varchar(10))
    insert into A values(1, 'Jerry', 'GuanZhuo' )
    create table B(ID int,name varchar(10),Address varchar(10))
    insert into B values(1, 'Joy', 'ShanHai ' )
    create table c(OrderID int,Name varchar(10))
    insert into c values(1, 'Jerry') 
    insert into c values(2, 'Joy') 
    goselect c.* , t.id , t.address
    from c left join (select * from a union all select * from b) t
    on c.name = t.namedrop table a,b,c/*
    OrderID     Name       id          address
    ----------- ---------- ----------- ----------
    1           Jerry      1           GuanZhuo
    2           Joy        1           ShanHai (2 行受影响)
    */