duanluanlang8501 2010-01-31 03:44
浏览 84
已采纳

在MYSQL中连接多个(4)表

I have four tables I want to join and get data from. The tables look something like...

  • Employees (EmployeeID, GroupID[fk], EmployeeName, PhoneNum)
  • Positions (PositionID, PositionName)
  • EmployeePositions (EployeePositionID, EmployeeID[fk], PositionID[fk])
  • EmployeeGroup (GroupID, GroupName)

[fk] = foreign key

I want to create a query that will return all the information about an employee(given by EmployeeID). I want a query that will return the given employees Name, position(s), and group in one row.

I think it needs to involve joins, but I am not sure how to format the queries. MYSQL's manual is technical beyond my comprehension. I would be very grateful for any help.

  • 写回答

2条回答 默认 最新

  • dongxing1853 2010-01-31 03:49
    关注

    It seems you have trouble with SQL, in general, rather than with mySQL in particular. The documentation of mySQL provides details about the various SQL expressions, but generally assume some familiarity with SQL. To get a quick start on SQL you may consider this W3schools.com primer.
    The query you need is the following.

    SELECT EmployeeName, PositionName, GroupName
    FROM Employees E
    LEFT JOIN EmployeePositions EP ON EP.EmployeeID = E.EmployeeID
    LEFT JOIN Positions P ON P.PositionID = EP.PositionId
    LEFT JOIN EmployeeGroup EG ON EG.GroupId = E.GroupId
    WHERE E.EmployeeId = some_value
    

    A few things to note:
    The 'LEFT' in 'LEFT JOIN' will result in producing NULL in lieu of PositionName or GroupName when the corresponding tables do not have a value for the given FK. (Should only happen if the data is broken, say if for example some employees have GroupId 123 but somehow this groupid was deleted from the EmployeeGroup table.
    The query returns one line per employee (1). You could use an alternative search criteria, for example WHERE EmployeeName = 'SMITH', and get a listing of all employees with that name. Indeed without a WHERE clause, you'd get a list of all employees found in Employees table.
    (1) that is assuming that each employee can only have one position. If somehow some employees have more than one position (i.e. multiple rows in EmployeePositions for a given EmployeeID), you'd get several rows per employee, the Name and Group being repeated and a distinct PostionName.

    Edit:
    If a given employee can have multiple positions, you can use the query suggested by Tor Valamo, which uses a GROUP BY construct, with GROUP_CONCAT() to pivot all the possible positions in one single field value in the returned row.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)