我有2个表收集并请求,当一个请求被创建它创建一个id,当这个人获得总金额的一部分,它与贷款ID一起收集表格,我需要回复请求信息以及收集的信息基于位于两个表上的loan_id然后在php上获取它。
收集了表格select * from request, users
where request.user_id = users.id and request.user_id = $user_id"
此选择获取请求表
select * from collected where loan_id = 146 (loan_id from request table)
我希望获得请求信息以及与请求表中的每个贷款ID相对应的收集表信息
答案 0 :(得分:2)
您可以使用Join(如果表列始终匹配,则使用内部联接)
select *
from request
inner join collected on request.loan_id = collected.loan_id
inner join users on request.user_id = users.id
Where request.user_id = $user_id
如果列有时不匹配,请
或离开加入
select *
from request
left join collected on request.loan_id = collected.loan_id
inner join users on request.user_id = users.id
Where request.user_id = $user_id