在mysql中连接相同的表

时间:2014-05-07 10:00:44

标签: mysql sql

这是我的选择查询,我想从同一个表中获取父人员姓名。

 SELECT 
     master.staffId,
     master.name,
     DATE_FORMAT(master.joinDate,'%d-%m-%Y') AS joinDate,
     master.commission,
     master.salary,place AS placeName,
     staffType,parent.name AS parentName 
  FROM 
     staff AS master
     LEFT JOIN place 
        ON place.placeId = master.placeId
     LEFT JOIN stafftype 
        ON stafftype.staffTypeId = master.staffTypeId
     left outer join staff AS parent 
        ON parent.staffId = master.staffId
                        ORDER BY name

1 个答案:

答案 0 :(得分:2)

在查看你的评论时,我认为你需要这个

 SELECT master.staffId,master.name,DATE_FORMAT(master.joinDate,'%d-%m-%Y') AS joinDate,master.commission,
                          master.salary,place AS placeName,staffType,parent.name AS parentName 
  FROM staff AS master
  LEFT JOIN place ON place.placeId = master.placeId
  LEFT JOIN stafftype ON stafftype.staffTypeId = master.staffTypeId
  left outer join staff AS parent ON parent.staffId = master.parentStaffId
  ORDER BY name