简体   繁体   English

在MYSQL中联接多个(4)表

[英]Joining multiple (4) tables in MYSQL

I have four tables I want to join and get data from. 我有四个表要联接并从中获取数据。 The tables look something like... 桌子看起来像...

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

[fk] = foreign key [fk] =外键

I want to create a query that will return all the information about an employee(given by EmployeeID). 我想创建一个查询,该查询将返回有关雇员的所有信息(由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. MYSQL的手册超出了我的理解范围。 I would be very grateful for any help. 我将非常感谢您的帮助。

It seems you have trouble with SQL, in general, rather than with mySQL in particular. 似乎您通常在使用SQL时遇到麻烦,而不是在使用mySQL时遇到麻烦。 The documentation of mySQL provides details about the various SQL expressions, but generally assume some familiarity with SQL. MySQL的文档提供了有关各种SQL表达式的详细信息,但通常假定您对SQL有所了解。 To get a quick start on SQL you may consider this W3schools.com primer . 为了快速开始使用SQL,您可以考虑使用W3schools.com入门
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. 当相应表没有给定FK的值时,“ LEFT JOIN”中的“ LEFT”将导致产生NULL来代替PositionName或GroupName。 (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. (仅在数据损坏时才会发生,例如,如果某些雇员具有GroupId 123,但不知何故从EmployeeGroup表中删除了此groupid。
The query returns one line per employee (1). 该查询为每位员工返回一行(1)。 You could use an alternative search criteria, for example WHERE EmployeeName = 'SMITH', and get a listing of all employees with that name. 您可以使用其他搜索条件,例如WHERE EmployeeName ='SMITH',并获取具有该名称的所有员工的列表。 Indeed without a WHERE clause, you'd get a list of all employees found in Employees table. 实际上,如果没有WHERE子句,您将在“雇员”表中找到所有雇员的列表。
(1) that is assuming that each employee can only have one position. (1)假设每个员工只能有一个职位。 If somehow some employees have more than one position (ie 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. 如果某些员工以某种方式拥有多个职位(即给定EmployeeID的EmployeePositions中有多行),则每个员工将获得几行,其中名称和组被重复,并且具有不同的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. 如果给定的雇员可以担任多个职位,则可以使用Tor Valamo建议的查询,该查询使用GROUP BY构造,并带有GROUP_CONCAT()在返回的行中的单个字段值中旋转所有可能的职位。

SELECT e.EmployeeID, e.EmployeeName, e.PhoneNum, 
       g.GroupName, GROUP_CONCAT(p.PositionName) AS Positions
FROM Employees e
  LEFT JOIN EmployeeGroup g ON g.GroupID = e.GroupID
  LEFT JOIN EmployeePositions ep ON ep.EmployeeID = e.EmployeeID
  LEFT JOIN Positions p ON p.PositionID = ep.PositionID
WHERE e.EmployeeID = 1
GROUP BY e.EmployeeID

Returns positions in a comma separated string on one row. 返回一行中用逗号分隔的字符串中的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM