![](/img/trans.png)
[英]Laravel Mysql Select from two tables and join result on a third table
[英]How to join 3 tables and select only max date on the third table
我正在尝试获取tbl_paid中的最新日期。 我已经获取了我想要的数据到前两个表,然后在第三个表中我只想获取最大日期。
我的第一个查询中有这个
SELECT * FROM tbl_client TC LEFT OUTER JOIN (SELECT c_id , MAX(transaction_num) MaxTransaction, MAX(loan_end) loan_end, collection FROM tbl_loan group by c_id) TL ON TL.c_id = TC.c_id
我只加入了两个表tbl_client c_id, acc_number, contact_num, fullname, address, remarks, penalty
和tbl_loan l_id, date_loan, loan_term, loan_end, collection, transaction_num, principal, interest, total_interest, total, payment, c_id
我想加入第三张表并从tbl_paid获取最新的pay_date,其中tbl_paid.c_id = tbl_loan.c_id和tbl_paid.transaction_num = tbl_loan.MaxTransaction_num
tbl_paid
p_id reciept_num金额pay_date c_id transaction_num
SELECT * FROM tbl_client TC LEFT OUTER JOIN (SELECT c_id, MAX(transaction_num) MaxTransaction, MAX(loan_end) loan_end, collection FROM tbl_loan group by c_id) TL ON TL.c_id = TC.c_id LEFT OUTER JOIN (SELECT c_id, Max(transaction_num), Max(pay_date) pay_date from tbl_paid group by c_id) TP ON TP.c_id = TC.c_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.