有2张表:如图,
关联关系为:
B.pil_id = A.id
B.stu_id = A.id
B.tea_id = A.id我要如何用sql语句连接查询把B表pil_id、stu_id、tea_id对应的名字都查询出来

解决方案 »

  1.   

    select *,
    (select name from a where id=pilid),
    (select name from a where id=stu_id),
    (select name from a where id=te_id)
    from b
      

  2.   

    select *,
    (select name from a where a.id=b.pilid),
    (select name from a where a.id=b.stu_id),
    (select name from a where a.id=b.te_id)
    from b
      

  3.   

    select *,
    (select name from a where a.id=b.pilid),
    (select name from a where a.id=b.stu_id),
    (select name from a where a.id=b.te_id)
    from b
      

  4.   

    select 
    (select name from a where a.id=b.pilid),
    (select name from a where a.id=b.stu_id),
    (select name from a where a.id=b.te_id)
    from b
      

  5.   

    select (select name from a where a.id=b.pilid) as pil_name,
                     (select name from a where a.id=b.stu_id) as stu_name,
                     (select name from a where a.id=b.tea_id) as tea_name
    from b 
      

  6.   

    如下:
    SELECT ax.pil_id,bx.name FROM b ax,a bx WHERE ax.pil_id=bx.Id
    UNION ALL
    SELECT ax.stu_id,bx.name FROM b ax,a bx WHERE ax.stu_id=bx.Id
    UNION ALL
    SELECT ax.tea_id,bx.name FROM b ax,a bx WHERE ax.tea_id=bx.Id
      

  7.   

    select (select name from A where A.Id= B.pil_id) as PilName,
    (select name from A where A.Id=B.stu_id) as StuName,
    (select name from A where A.Id=B.tea_id)as TeaName from B