0

我有一个这样的mysql查询

SELECT s.admission_number, s.student_name, c.class, m.month, 
       f.fee, fr23.amount_received
FROM students s
LEFT JOIN fee_receipts fr ON s.id = fr.admission_number
LEFT JOIN fee_receipts_23_repeat fr23 ON fr.id = fr23.parent_id
JOIN class c ON s.class = c.id
LEFT JOIN fee f ON f.id = fr23.fee
LEFT JOIN months m ON m.id = fr23.month

哪个显示这样的结果?

ad_num  student_name class   month    fee   amount_received 
779     A Jain      Nur     January Tuition   Fee   575.00
808     A Gupta     Nur     January Tuition   Fee   0.00
821     A Mohanty   Nur     NULL    NULL            NULL
818     ATiwari     Nur     FebruaryTuition   Fee   575.00
826     D Mishra    Nur     NULL    NULL      NULL
813     G Kaur      Nur     NULL    NULL      NULL  NULL
809     P Palai     Nur     NULL    NULL      NULL
822     S Agrawal   KG1     December Tuition  Fee   575.00
773     S Garg      KG1     NULL    NULL            NULL

现在我试图通过排除那些已经支付但没有得到正确结果的学生来按班级获取未支付费用的学生名单。我添加到上述查询的条件是这样的

where fr23.amount_received is NULL and c.class = 'Nur' and m.month != 'January'

我希望它会列出所有尚未支付一月份费用的学生的结果,但它给了我一个空的结果集。

实际上,我正在尝试查找没有付款条目的学生,这意味着月份、费用和金额列将具有空值。

我不太确定我是否采取了正确的方法?

4

2 回答 2

1

将您的条件移动到JOIN ON条件中:

SELECT s.admission_number, s.student_name, c.class, m.month, 
       f.fee, fr23.amount_received
FROM students s
LEFT JOIN fee_receipts fr ON s.id = fr.admission_number
LEFT JOIN fee_receipts_23_repeat fr23 ON fr.id = fr23.parent_id
    AND fr23.amount_received is NULL   -- Moved to here
JOIN class c ON s.class = c.id
LEFT JOIN fee f ON f.id = fr23.fee
LEFT JOIN months m ON m.id = fr23.month
    AND (m.month != 'January' OR m.month IS NULL) -- Added IS NULL check
于 2013-02-26T18:52:36.650 回答
0

经典的空陷阱!

空比较并不像你想象的那样......

change m.month != 'January'

to

coalesce(m.month,'') != 'January'
于 2013-02-26T18:53:04.280 回答