0

I am creating a simple gim system payment reminder calculation part. I want to get the result of those who are not paid the amount of the current month whose people name need to be displayed. What I tried so far I attached below.

Member table

id   mno    pname       dob 
1   e001   john     1991.12.3
2   e002   steve     1987.11.3
3   e003   sunm     1986.11.31
4   e004   jeba      1981.1.3

Payment table

id   mno     paydate       amount     months
1   e001   2020-12-14      20000       12 

Months table 12 means - December month I got the SQL code of current month MONTH(getdate())

I tried query like this

  SELECT Member.mno
    ,Member.pname
    ,payment.amount
FROM Member
LEFT JOIN payment ON Member.mno = payment.mno
WHERE payment.amount IS NULL
    AND payment.months = MONTH(getdate())

When I run the query I didn't get any result. I want to get the result of those who are not paid the amount of the current month whose people name need to be displayed. What was the problem with the above query?

Dharman
  • 30,962
  • 25
  • 85
  • 135
creative one2018
  • 131
  • 2
  • 11

1 Answers1

1

You need to move your condition from WHERE clause to ON clause as follows:

SELECT MEMBER.MNO,
       MEMBER.PNAME,
       PAYMENT.AMOUNT
  FROM MEMBER LEFT JOIN PAYMENT ON MEMBER.MNO = PAYMENT.MNO
                                AND PAYMENT.MONTHS = MONTH(GETDATE())
 WHERE PAYMENT.AMOUNT IS NULL
Popeye
  • 35,427
  • 4
  • 10
  • 31