PHP MySQL Join users table to multiple columns and rows

I'm having some problems with tables in a project I am working on for projects.

I have 3 tables, one for projects, one for clients and one for users. I am trying to store the client and user id in the Projects table and join them on checkout.

+----+--------------+
| 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 with Select all day and this is as far as I could find 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 helps me with this:

+---------------+--------------+-----------------+-------------------+-------------------+--------------------+
| 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 my life, I can't get ProjectElectrical and ProjectMechanical to do the same as ProjectLead. I either get duplicate ProjectLead or I get NULL.

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 have scripted it on SQL Fiddle

Thanks in advance for any answers!

+3


source to share


1 answer


Here is the updated script request:



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

      

+2


source







All Articles