我有这张桌子(付款)
| id | amount | payment | frequency |
|----|--------|------------|-----------|
| 1 | 100 | 2017-05-15 | 12 |
| 2 | 200 | 2017-05-15 | 12 |
| 3 | 300 | 2017-05-17 | 4 |
这一个(payments_log):
| id | payment_id | payment |
|----|------------|------------|
| 1 | 1 | 2017-04-15 |
| 2 | 1 | 2017-05-15 |
| 3 | 2 | 2017-04-15 |
这是我的问题:
SELECT p.id, p.amount, p.date AS payment_date, pl.date
FROM payments AS p
LEFT JOIN payments_log AS pl ON pl.payment_id = p.id
WHERE p.frequency = 12 AND
DATE_FORMAT(p.date,'%m-%d') IN ('06-15','07-15','08-15','09-15','10-15','11-15','12-15','01-15','02-15','03-15','04-15','05-15')
我只想返回匹配付款日期的记录,但如果payment_log表中有任何记录与当前日期,则跳过主表中的任何记录:
| id | amount | payment | frequency |
|----|--------|------------|-----------|
| 2 | 200 | 2017-05-15 | 12 |
ID 2有效,因为与' 04-15'匹配日期,但记录ID 1无效,因为有一个payment_log与当前日期。
感谢。