简体   繁体   English

PHP MySQL将用户表连接到多个列和行

[英]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. 我有3个表,一个用于项目,一个用于客户,一个用于用户。 I'm trying to store the ID of the Customers and Users in the Projects table, and join them when retrieving. 我正在尝试在Projects表中存储Customers和Users的ID,并在检索时加入它们。

+----+--------------+
| 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,这就是我能够搜索到的:

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. 但对于我的生活,我不能让ProjectElectrical和ProjectMechanical做与ProjectLead相同的事情。 I either get duplicates of ProjectLead, or I get NULLs. 我要么重复ProjectLead,要么得到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've fiddle'd it at SQL Fiddle 我在SQL Fiddle上搞砸了它

Thanks in advance for any and all replies! 在此先感谢所有回复!

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

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

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