通过join获取第二个表中不存在的记录

时间:2012-05-30 06:44:55

标签: mysql null left-join

我需要以下结果集中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需要通过其他类型的连接进行更改。检查它.....

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

试过这个:

SELECT * FROM TableA
LEFT OUTER JOIN TableB
ON TableA.name = TableB.name
WHERE TableB.id IS null

要仅在表A中生成记录集,而不在表B中生成记录集,我们执行相同的左外连接,然后通过where子句从右侧排除我们不想要的记录。

http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html

答案 1 :(得分:0)

我相信你可以简单地添加一个WHERE子句“AND 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