如何根据两个表中的id从2个表中选择内容(how to select content from 2 tables based on id that are in both tables)

编程入门 行业动态 更新时间:2024-10-22 21:24:39
如何根据两个表中的id从2个表中选择内容(how to select content from 2 tables based on id that are in both tables)

我有2个表收集并请求,当一个请求被创建它创建一个id,当该人获得总金额的一部分,它与贷款ID一起收集表,我需要回应请求信息以及基于loan_id收集的信息位于两个表上然后在PHP上获取此。

表请求

提交收集

select * from request, users where request.user_id = users.id and request.user_id = $user_id"

这个select获取请求表

select * from collected where loan_id = 146 (loan_id from request table)

我想要请求信息以及与请求表中的每个贷款ID相对应的收集的表信息

i have 2 tables collected and request, when a request is made it create an id, when the person get part of total amount it goes to collected tabled along with the loan id, i need to echo request info along with collected info based on loan_id that located on both tables then fetch this on php.

table request

tabled collected

select * from request, users where request.user_id = users.id and request.user_id = $user_id"

this select gets the request table

select * from collected where loan_id = 146 (loan_id from request table)

i will like to have request info along with collected table info corresponding to each loan id from request table

最满意答案

您可以使用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

You can use Join (inner join if the table column always match)

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

or left join if the column sometime don't match

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

更多推荐

本文发布于:2023-08-04 10:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415303.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两个   内容   个表中   id   based

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!