2012-05-30 41 views
1

這是不存在的第二個表我需要的記錄,其中pid是從以下結果集NULL記錄:獲取與加入

pid users_fee_schedule_students_name users_fee_schedule_students_uid 

8  users     6   MBA in IT 1 1337097600 3 6 user 250000 
9  users     6   MBA in IT 2 1337184000 3 7 user 250000 
NULL ashuser     277   MBA in IT 1 1337097600 3 6 user 250000 
NULL ashuser     277   MBA in IT 2 1337184000 3 7 user 250000 
10  sriuser     66   MBA in IT 1 1337097600 3 6 user 250000 
NULL sriuser     66   MBA in IT 

我SQL查詢來獲取上述結果集

SELECT FSP.pid, users_fee_schedule_students.name AS users_fee_schedule_students_name, users_fee_schedule_students.uid AS users_fee_schedule_students_uid, fee_schedule.name AS fee_schedule_name, fee_schedule_instalments.instalment_no AS fee_schedule_instalments_instalment_no, fee_schedule_instalments.payable_by AS fee_schedule_instalments_payable_by, fee_schedule.fid AS fid, fee_schedule_instalments.iid AS fee_schedule_instalments_iid, 'user' AS field_data_field_school_course_user_entity_type, SUM(fee_schedule_instalments.amount) AS fee_schedule_instalments_amount 
FROM 
ic_fee_schedule AS fee_schedule 
LEFT JOIN ic_fee_schedule_students AS fee_schedule_students ON fee_schedule.fid = fee_schedule_students.fid 
LEFT JOIN ic_users AS users_fee_schedule_students ON fee_schedule_students.uid = users_fee_schedule_students.uid 
LEFT JOIN ic_fee_schedule_instalments AS fee_schedule_instalments ON fee_schedule.fid = fee_schedule_instalments.fid 
LEFT JOIN ic_fee_schedule_payments AS FSP ON fee_schedule.fid = FSP.fid AND fee_schedule_students.uid = FSP.uid AND fee_schedule_instalments.iid = FSP.iid 
WHERE (((fee_schedule.fid = '3'))) 
GROUP BY users_fee_schedule_students_name, users_fee_schedule_students_uid, fee_schedule_name, fee_schedule_instalments_instalment_no, fee_schedule_instalments_payable_by, fid , fee_schedule_instalments_iid 

我認爲 LEFT JOIN ic_fee_schedule_payments AS FSP需要改變一些其他類型的連接。檢查它.....

在此先感謝您的幫助。

+1

請使用SQL小提琴把你的結構,並明確告訴我們,你到底想 –

+0

遺憾沒得到什麼是SQL小提琴? –

+1

http://sqlfiddle.com/點擊它,你就會知道 –

回答

0

我相信你可以簡單地添加一個WHERE子句「和FSP.pid IS NULL」因爲你做左連接上FSP。

試試這個:

SELECT FSP.pid, users_fee_schedule_students.name AS users_fee_schedule_students_name, users_fee_schedule_students.uid AS users_fee_schedule_students_uid, fee_schedule.name AS fee_schedule_name, fee_schedule_instalments.instalment_no AS fee_schedule_instalments_instalment_no, fee_schedule_instalments.payable_by AS fee_schedule_instalments_payable_by, fee_schedule.fid AS fid, fee_schedule_instalments.iid AS fee_schedule_instalments_iid, 'user' AS field_data_field_school_course_user_entity_type, SUM(fee_schedule_instalments.amount) AS fee_schedule_instalments_amount 
FROM ic_fee_schedule AS fee_schedule 
LEFT JOIN ic_fee_schedule_students AS fee_schedule_students ON fee_schedule.fid = fee_schedule_students.fid 
LEFT JOIN ic_users AS users_fee_schedule_students ON fee_schedule_students.uid = users_fee_schedule_students.uid 
LEFT JOIN ic_fee_schedule_instalments AS fee_schedule_instalments ON fee_schedule.fid = fee_schedule_instalments.fid 
LEFT JOIN ic_fee_schedule_payments AS FSP ON fee_schedule.fid = FSP.fid AND fee_schedule_students.uid = FSP.uid AND fee_schedule_instalments.iid = FSP.iid 
WHERE fee_schedule.fid = '3' 
AND FSP.pid IS NULL 
GROUP BY users_fee_schedule_students_name, users_fee_schedule_students_uid, fee_schedule_name, fee_schedule_instalments_instalment_no, fee_schedule_instalments_payable_by, fid , fee_schedule_instalments_iid