[英]MySQL Join Multiple (More than 2) Tables with Conditions
Assume I have 4 tables:假设我有 4 张桌子:
Table 1: Task
表 1:
Task
ID Task Schedule
1 Cut Grass Mon
2 Sweep Floor Fri
3 Wash Dishes Fri
Table 2: Assigned
表 2:
Assigned
ID TaskID (FK) PersonID (FK)
1 1 1
2 1 2
3 2 3
4 3 2
Table 3: Person
表 3:
Person
ID Name
1 Tom
2 Dick
3 Harry
Table 4: Mobile
表 4:
Mobile
ID PersonID (FK) CountryCode MobileNumber
1 1 1 555-555-5555
2 2 44 555-555-1234
3 3 81 555-555-5678
4 3 81 555-555-0000
I'm trying to display the我正在尝试显示
I think it should be something like the following, but I'm not sure how to set up the conditions so that the results are limited correctly:我认为它应该类似于以下内容,但我不确定如何设置条件以正确限制结果:
SELECT T.ID, T.Task, P.Name, M.MobileNumber
FROM Task AS T
LEFT JOIN Assigned AS A
ON T.ID = A.TaskID
LEFT JOIN Person AS P
ON A.PersonID = P.ID
LEFT JOIN Mobile AS M
ON M.PersonID = P.ID
WHERE T.Schedule = Fri
My goal is to fetch the following information (it will be displayed differently):我的目标是获取以下信息(显示方式不同):
Tasks Name MobileNumber
Sweep Floor, Wash Dishes Dick, Harry 44-555-555-1234, 81-555-555-5678, 81-555-555-0000
Of course, if JOIN is the wrong way to do this, please say so.当然,如果 JOIN 是错误的做法,请说出来。
It's unclear what you want to do with duplicate data in this case, but you should be looking at using inner joins instead of outer joins, and using something like group_concat() to combine the phone numbers.目前尚不清楚在这种情况下您要如何处理重复数据,但您应该考虑使用内部联接而不是外部联接,并使用诸如 group_concat() 之类的东西来组合电话号码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.