I have the following tables:
table name:: student
studentid studentname
1001 Charlie Sheen
1002 John Cryer
table name:: studentpayment1
paymentid studentid fee_month fee_year totalamount
1234 1001 February 2012 $500
4321 1002 January 2012 $1500
table name:: studentpayment2
id pid fee_type fee_amount
1 1234 Monthly Fee $500
2 4321 Exam Fee $1500
Now, I am trying to find out the name of the students who hasn't paid the "Monthly Fee" for the month of February - year 2012.
I tried this following code but it doesn't generate the correct information.
$year="2012"; $month="February"; $fee_type="Monthly Fee";
SELECT DISTINCT studentid,
studentname
FROM student
WHERE NOT EXISTS (SELECT *
FROM studentpayment1
JOIN studentpayment2
ON studentpayment1.paymentid = studentpayment2.pid
WHERE fee_month = '$month'
AND fee_type = '$fee_type'
AND fee_year = '$year'
AND student.studentid = studentpayment1.studentid)
LIMIT $perPage
How do I fix this query?