我可以跨共享多个主键的3个表执行mysql查询吗?
我使用这个查询来获取所有用户id请求,并从与检索到的记录相关联的配置文件id中获取配置文件名称我可以跨共享多个主键的3个表执行mysql查询吗?,mysql,Mysql,我使用这个查询来获取所有用户id请求,并从与检索到的记录相关联的配置文件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, Profi
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表中检索用户id的名称,该怎么办?我猜用户id是某些用户表中的主键,而您想要的表也有一个用户id引用,指示此记录属于该用户。
使用。例如,您希望在users.user\u id=groups.user\u 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
像这样的
select
r.request_id
, r.user_id
, r.profile_id
, r.job_title
, r.date
, r.time
, r.info
, r.approval
, p.first_name
, p.last_name
, a.something
from Requests as r
join Profile as p on p.profile_id = r.profile_id
join Accounts as a on a.user_id = r.user_id
where user_id = '$user_id'
order by r.approval asc ;