tags:

views:

111

answers:

2

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.

+5  A: 

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.

mjv
This query returns several rows, while he wants one row. ;)
Tor Valamo
@Tor Valamo. If the data is as it seems it should be the above query will return one row per Employee. (The assumptions are: a given employee only has one Position and the PositionId and GroupId lookups return one record each, per their respective ID)
mjv
EmployeePositions is a many-to-many relationship... and the OP also says that. "... a query that will return the given employees Name, **position(s)**, and group **in one row**."
Tor Valamo
mjv is correct. Although I suppose it is possible that multiple rows could be returned if someone entered data in the database to make it do so, the reality is there wont be situations where that is happening.
Paul
+1  A: 
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.

Tor Valamo