有这一个表:employeeid (员工ID) postionid 职位ID standerid 住宿标准ID departid 部门ID   ,其中这三个字 postionid 职位ID standerid 住宿标准ID departid 部门ID 均来自三个表中。其各表中都有其名称。如  职位名称。住宿标准名称,部门名称;
在grid中,实现 员工编号 ,职位名称,住宿标准名称,部门名称,请问该如何写一个好的SQL语句才能实现。

解决方案 »

  1.   


    select employeeid
    ,(select  职位名称 from  职位表 where 职位ID = table.postionid ) as 职位名称
    ,(select  住宿标准名称 from  住宿标准表 where 住宿标准ID = table.standerid ) as 住宿标准名称 
    ,(select  部门名称 from  部门 where 部门ID = table.departid ) as 部门名称
    from 表 as table也可以使用左联接,但是个人感觉如果那三个表只取名称的话,还是用子查询好点。
      

  2.   

    select 员工编号,职位编号,住宿标准名称 from A inner join B on A.postionid = B.postionid inner join C on A.standerid = C.standerid inner join D on A.departid = D.departid where employeeid = 'xxxxx'
      

  3.   

    不是只取名字.在grid中,实现 员工编号 ,职位名称,住宿标准名称,部门名称,待遇,入职日期.等;以上这二种哪一种更好呢..
      

  4.   

    select  a employeeid,c.职位名称,b.部门,d.住宿标准名称 from 
    职位表  a left join 部门表  b on a.employeeid=b.employeeid
    left join 职位表  on a.employeeid=c.employeeid left join 
    住宿表 d on a.employeeid=d.employeeid
      

  5.   

    请教下楼主的SQL数据规模,如果只是规模在万行左右的话,楼上做子查询和做级联的都行。
    如果规模超过10万行的话,建议在员工表上添加职位名称,住宿标准名称,部门名称字段。在插入,删除和修改职位,住宿和部门表的时候,通过存储过程(也可以使用触发器,方便不过性能不如存储过程),同步修改员工表上的相应字段。
    4表级联或者子查询的性能损耗太大了。