我有一个问题。我有4个表,即:学生,费用,付款和课程
学生
( student_id ,名字,姓氏,手机,电子邮件,地址, photofile, class_id )
类
( class_id ,class_description,class_date,class_venue)
付款
(payment_id, student_id ,payment_date,payment_amount, fee_id )
费用
( fee_id ,fee_description, class_id ,fee_amount)
基本上,我需要创建一个查询,该查询能够显示学生是否根据 {{1}制作了 payments
}} 即可。但是,我已尝试从stackoverflow获得的所有左连接,但仍无法获得我想要的结果。
我有这个:
fees
然后我得到了如下结果:
SELECT
students.student_id, fee.fee_id, fee.fee_description,
fee.class_id, fee.fee_amount, students.firstname, students.lastname,
payments.payment_id, payments.payment_date
FROM students
LEFT JOIN class ON students.class_id = class.class_id
LEFT JOIN fee ON fee.class_id = class.class_id
LEFT JOIN payments ON payments.student_id = students.student_id
问题:
目前,我正在使用Flash Builder开发移动应用。该应用程序用于跟踪学生的付款,因此,我需要先解决此问题,然后再转到其他人。如果有人知道如何,请通过这个指导我。提前谢谢><“
源mysql代码在这里:http://sqlfiddle.com/#!2/d4482a
答案 0 :(得分:1)
加入付款和费用表,以便为您提供正确的结果
修改强>
SELECT
students.student_id, fee.fee_id, fee.fee_description,
fee.class_id, fee.fee_amount, students.firstname, students.lastname,
payments.payment_id, payments.payment_date
FROM students
LEFT JOIN class ON students.class_id = class.class_id
LEFT JOIN fee ON fee.class_id = class.class_id
LEFT JOIN payments ON payments.student_id = students.student_id and Payments.fee_id = fee.fee_id
这将为您提供所有已付款且未付款的学生 您可以使用payment_id为null或根据您的需要不为空来过滤它 如果payment_id为空,则表示他们没有付款并以其他方式支付
答案 1 :(得分:0)
试试这个。
SELECT a.student_id,d.fee_id, d.fee_description, b.class_id,d.fee_amount
from Students a, Class b, Payments c, Fee d
where a.class_id=b.class_id and b.class_id=d.class_id and a.student_id=c.student_id
and c.payment_amount=d.fee_amount;
答案 2 :(得分:0)
将类加入费用的重点是什么,而不是尝试这个..虽然我没有测试过这个
SELECT
students.student_id, fee.fee_id, fee.fee_description,
fee.class_id, fee.fee_amount, students.firstname, students.lastname,
payments.payment_id, payments.payment_date
使用
FROM students
LEFT JOIN payments ON payments.student_id = students.student_id
LEFT JOIN fee ON fee.fee_id = payments.fee_id
LEFT JOIN class ON students.class_id = class.class_id
如果想要将Class id作为费用表
,请选中此项FROM students
LEFT JOIN payments ON payments.student_id = students.student_id
LEFT JOIN class ON students.class_id = class.class_id
LEFT JOIN fee ON fee.fee_id = payments.fee_id AND fee.class_id = class.class_id