我有这个查询来获取 user_id 的所有请求,并从与检索到的记录关联的 profile_id 中获取配置文件名称。
SELECT Requests.request_id,Requests.user_id,Requests.profile_id,Requests.job_title,Requests.date,Requests.time,Requests.info,Requests.approval, Profile.first_name, Profile.last_name FROM Requests, Profile WHERE user_id = '$user_id' AND Requests.profile_id = Profile.profile_id ORDER BY approval ASC
如果我想添加另一个表来从 Accounts 表中检索 user_id 的名称该怎么办?
最佳答案
我认为使用联接是一个好主意,因为它更清晰。
SELECT Requests.request_id,Requests.user_id,Requests.profile_id,Requests.job_title,Requests.date,Requests.time,Requests.info,Requests.approval, Profile.first_name, Profile.last_name, OtherTable.other_field FROM Requests
LEFT JOIN Profile ON Requests.profile_id = Profile.profile_id
LEFT JOIN OtherTable ON Requests.profile_id = OtherTable.profile_id
WHERE Request.user_id = '$user_id' ORDER BY approval AS
关于mysql - 我可以跨 3 个共享多个主键的表进行 mysql 查询吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4617558/