cwaddilove
cwaddilove

Reputation: 67

PHP MySQL Join Users Table to Multiple Columns & Rows

I've been having some issues joining tables in a project I'm working on, for projects.

I have 3 tables, one for Projects, one for Customers, and one for Users. I'm trying to store the ID of the Customers and Users in the Projects table, and join them when retrieving.

+----+--------------+
| ID | CustomerName |
+----+--------------+
|  1 | Customer 1   |
|  2 | Customer 2   |
|  3 | Customer 3   |
+----+--------------+

+----+-----------+----------+
| ID | FirstName | LastName |
+----+-----------+----------+
|  1 | Bob       | Belcher  |
|  2 | Stirling  | Archer   |
|  3 | Bart      | Simpson  |
|  4 | Peter     | Griffin  |
|  5 | BoJack    | Horseman |
|  6 | Eric      | Cartman  |
+----+-----------+----------+

+----+---------------+-----------------+-------------+-------------------+-------------------+--------------------+
| ID | ProjectNumber | ProjectCustomer | ProjectLead | ProjectElectrical | ProjectMechanical | ProjectDescription |
+----+---------------+-----------------+-------------+-------------------+-------------------+--------------------+
|  1 | 0001          |               1 |           3 |                 4 |                 6 | Project 1          |
|  2 | 0002          |               2 |           2 |                 5 |                 5 | Project 2          |
|  3 | 0003          |               3 |           1 |                 6 |                 4 | Project 3          |
+----+---------------+-----------------+-------------+-------------------+-------------------+--------------------+

I've been playing around with Select's all day and this is as far as I've been able to get searching SO:

select Projects.ProjectNumber, Customers.CustomerName, CONCAT_WS(' ', Users.FirstName, Users.LastName) AS ProjectLead, Projects.ProjectElectrical, Projects.ProjectMechanical, Projects.ProjectDescription FROM Projects
INNER JOIN Customers ON Projects.ProjectCustomer = Customers.ID
LEFT JOIN Users ON Projects.ProjectLead = Users.ID

Which gets me part of the way there:

+---------------+--------------+-----------------+-------------------+-------------------+--------------------+
| ProjectNumber | CustomerName | ProjectLead     | ProjectElectrical | ProjectMechanical | ProjectDescription |
+---------------+--------------+-----------------+-------------------+-------------------+--------------------+
| 0001          | Customer 1   | Bart Simpson    |                 4 |                 6 | Project 1          |
| 0002          | Customer 2   | Stirling Archer |                 5 |                 5 | Project 2          |
| 0003          | Customer 3   | Bob Belcher     |                 6 |                 4 | Project 3          |
+---------------+--------------+-----------------+-------------------+-------------------+--------------------+

But for the life of me, I can't get ProjectElectrical and ProjectMechanical to do the same thing as ProjectLead. I either get duplicates of ProjectLead, or I get NULLs.

Can anyone help point me in the right direction? Do I need to completely redesign my query or am I on the right track?

I've fiddle'd it at SQL Fiddle

Thanks in advance for any and all replies!

Upvotes: 3

Views: 586

Answers (1)

AlexL
AlexL

Reputation: 1707

Here is the updated query from the fiddle:

select Projects.ProjectNumber, Customers.CustomerName, CONCAT_WS(' ', Users.FirstName, Users.LastName) AS ProjectLead, CONCAT_WS(' ', u2.FirstName, u2.LastName) AS ProjectElectrical, CONCAT_WS(' ', u3.FirstName, u3.LastName) AS ProjectMechanical, Projects.ProjectDescription FROM Projects
INNER JOIN Customers ON Projects.ProjectCustomer = Customers.ID
LEFT JOIN Users ON Projects.ProjectLead = Users.ID
LEFT JOIN Users AS u2 ON Projects.ProjectElectrical = u2.ID
LEFT JOIN Users AS u3 ON Projects.ProjectMechanical = u3.ID

Upvotes: 2

Related Questions