複数のテーブルのデータを結合する仕事を与えられました。その一部は、「ユニオン」を含む前に行われます。次に、「migrated_loan_account」テーブルからデータを取得する必要があります。だから私はそれに参加しました。しかし、フィールドを追加しても機能しません。この 4 つのフィールドにコメントすると、機能します。私は何をすべきか理解していません。誰でもこれについて私を助けてくれますか?! 以下は私のコードです:
` SELECT -- office_info.office_ref_code AS old_office_code
-- , office_info.office_code
-- , office_info.office_name
-- , project_info.project_ref_code AS old_project_code
-- , project_info.project_code
-- , project_info.project_name
-- ,
IFNULL(group_info.group_reference_number, '') AS old_group_code
, IFNULL(RIGHT(group_info.group_code, 5), '') AS group_code
, IFNULL(group_info.group_name, '') AS group_name
, IFNULL(member_info.reference_no, '') AS old_member_code
, RIGHT(member_info.member_no, 5) AS member_code
, member_info.member_name
, IFNULL(savings_account.savings_balance,0) AS savings_balance
, la.account_ref_no AS old_loan_no
, la.account_no AS loan_no
, loan_status.name AS loan_status
, DATE(la.disbursement_date) AS disbursement_date
, IFNULL(la.disbursed_amount,0) AS disbursed_amount
, IFNULL(la.outstanding_balance,0) AS loan_due
, IFNULL(la.principal_outstanding,0) AS principal_outstanding
, IFNULL(la.interest_outstanding,0) AS interest_outstanding
, IFNULL(la.interest_realizable,0) AS interest_realizable
, IFNULL(la.overdue_amount,0) AS overdue_amount
, IFNULL(migrated_loan_account.outstanding_balance, 0) AS radar_loan_due
, IFNULL(migrated_loan_account.principal_outstanding, 0) AS radar_principal_outstanding
, IFNULL(migrated_loan_account.interest_realizable, 0) AS radar_interest_realizable
, IFNULL(migrated_loan_account.overdue_amount, 0) AS radar_overdue_amount
FROM member_info
INNER JOIN office_info ON
(office_info.id = member_info.branch_info_id)
INNER JOIN country_head_office ON
(country_head_office.id = office_info.country_head_office_id)
INNER JOIN country ON
(country.id = country_head_office.office_country_id)
INNER JOIN project_info ON
(project_info.id = member_info.project_info_id)
INNER JOIN country_program_info ON
(country_program_info.id = project_info.program_info_id)
INNER JOIN program_info ON
(program_info.id = country_program_info.program_info_id)
LEFT JOIN group_info ON
(group_info.id = member_info.group_info_id)
LEFT JOIN savings_account ON
(savings_account.member_info_id= member_info.id)
LEFT JOIN loan_account AS la ON
(la.member_id = member_info.id)
INNER JOIN
(
SELECT member_id, MAX(disbursement_date) AS max_date
FROM loan_account
GROUP BY member_id
) mla ON la.member_id = mla.member_id AND la.disbursement_date = mla.max_date
LEFT JOIN loan_status ON
(loan_status.id = la.loan_status_id)
LEFT JOIN migrated_loan_account ON
(migrated_loan_account.loan_account_id = la.id)
WHERE country.id = 1 AND 1=1 AND project_info.id = 'BI0000000000000000000001' AND office_info.id = 'BI0000000000000000000363'
UNION
SELECT -- office_info.office_ref_code AS old_office_code
-- , office_info.office_code
-- , office_info.office_name
-- , project_info.project_ref_code AS old_project_code
-- , project_info.project_code
-- , project_info.project_name
-- ,
IFNULL(group_info.group_reference_number, '') AS old_group_code
, IFNULL(RIGHT(group_info.group_code, 5), '') AS group_code
, IFNULL(group_info.group_name, '') AS group_name
, IFNULL(member_info.reference_no, '') AS old_member_code
, RIGHT(member_info.member_no, 5) AS member_code
, member_info.member_name
, IFNULL(savings_account.savings_balance,0) AS savings_balance
, 'NO LOAN' AS old_loan_no
, '' AS loan_no
, '' AS loan_status
, NULL AS disbursement_date
, 0 AS disbursed_amount
, 0 AS loan_due
, 0 AS principal_outstanding
, 0 AS interest_outstanding
, 0 AS interest_realizable
, 0 AS overdue_amount
FROM member_info
INNER JOIN office_info ON
(office_info.id = member_info.branch_info_id)
INNER JOIN country_head_office ON
(country_head_office.id = office_info.country_head_office_id)
INNER JOIN country ON
(country.id = country_head_office.office_country_id)
INNER JOIN project_info ON
(project_info.id = member_info.project_info_id)
INNER JOIN country_program_info ON
(country_program_info.id = project_info.program_info_id)
INNER JOIN program_info ON
(program_info.id = country_program_info.program_info_id)
LEFT JOIN group_info ON
(group_info.id = member_info.group_info_id)
LEFT JOIN savings_account ON
(savings_account.member_info_id= member_info.id)
WHERE country.id = 1 AND 1=1 AND project_info.id = 'BI0000000000000000000001' AND office_info.id = 'BI0000000000000000000363' AND member_info.id NOT IN (SELECT loan_account.member_id FROM loan_account)
ORDER BY -- old_office_code, old_project_code,
old_group_code, old_member_code;
`
次のフィールドにコメントすると機能します。急いで助けてください...
/*, ifnull(migrated_loan_account.outstanding_balance, 0) as radar_loan_due
, ifnull(migrated_loan_account.principal_outstanding, 0) as radar_principal_outstanding
, ifnull(migrated_loan_account.interest_realizable, 0) as radar_interest_realizable
, ifnull(migrated_loan_account.overdue_amount, 0) as radar_overdue_amount*/