开发者

Joining multiple (4) tables in MYSQL

开发者 https://www.devze.com 2022-12-18 09:46 出处:网络
I have four tables I want to join and get data from. The tables look something like... Employees (EmployeeID, GroupID[fk], EmployeeName, PhoneNum)

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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消