2016-05-06 7 views
0

次の図に示すように、私はすべての顧客にローンの有無をフィルタリングする必要があります。mysqlがより多くの結合テーブルを使用して共用を実行します

enter image description here

は、ここに私のmysqlのクエリです:

SELECT 
    cum.id, 
    lhh.mst_customer_id, 
    cum.first_name, 
    cum.middle_name, 
    cum.last_name, 
    cum.name_ins, 
    lhh.loan_amount, 
    lhh.date_granted, 
    lhh.loan_duration, 
    lhh.status 
FROM 
    loan_management_app.trans_cus_has_loan_header lhh 
LEFT JOIN 
    loan_management_app.mst_customer cum 
ON 
    (
     lhh.mst_customer_id = cum.id) 
UNION 
SELECT 
    cum.id, 
    lhh.mst_customer_id, 
    cum.first_name, 
    cum.middle_name, 
    cum.last_name, 
    cum.name_ins, 
    lhh.loan_amount, 
    lhh.date_granted, 
    lhh.loan_duration, 
    lhh.status 
FROM 
    loan_management_app.trans_cus_has_loan_header lhh 
RIGHT JOIN 
    loan_management_app.mst_customer cum 
ON 
    (
     lhh.mst_customer_id = cum.id) 
INNER JOIN 
     loan_management_app.mst_loan lon 
ON 
    (
     lhh.mst_loan_id= lon.id) 
INNER JOIN 
     loan_management_app.trans_loan_has_interest lhi 
ON 
     (lhi.mst_loan_id=lon.id) 
INNER JOIN loan_management_app.mst_interest mi 
ON 
     (mi.id=lhi.mst_interest_id) ; 

しかし、それはまだかなりのだろうこれについてloans.Anyの助けを借りて、人々だけを返します。前もって感謝します。

答えて

2

あなたはmst_customerでLEFT JOINを実行しています。顧客がいなくてもすべてのloan_managementを取得しています。

LEFT JOIN 
    loan_management_app.mst_customer cum 

RIGHT多分JOINを使用してみてください:

RIGHT JOIN 
    loan_management_app.mst_customer cum 

また、あなたがそのクエリを使用することができ、すべてのレフトで 'mst_customer' から行くこと合流。そのような何か:

SELECT * FROM mst_customer mc 
LEFT JOIN trans_cus_has_loan_header tchl ON mc.id = tchl.mst_customer_id 
LEFT JOIN mst_loan ml ON ml.id = tchl.mst_loan_id 
LEFT JOIN trans_loan_has_interest tlhi ON tlhi.mst_loan_id = ml.id 
LEFT JOIN mst_interest mi ON mi.id = tlhi.mst_interest_id 

私はこの最後の1が:-)

+0

はいそれは働くあなたのために働くことを願っています..ありがとうございました:) –

+0

あなたはそのような場合のためにUNIONを必要としません。より多くの行(および同じ列)を持つ別の顧客テーブルがある場合は、customers_2とのUNION ALLを実行して、これらの2つのテーブルのすべての行を取得する必要があります。よろしく。 – Grommy

関連する問題