I want to join four tables and get records older than 30 days. I think I'm doing fine but not in the date older than 30 days.

it returns empty results.
my query: 

```
SELECT b.accountid, 
       a.memberid, 
       c.id,
       d.memberid AS uid,
       d.datecreated 
FROM   `tbl_members` c 
       LEFT JOIN `table1` b 
              ON c.`id` = b.`accountid` 
       LEFT JOIN `table2` a 
              ON b.`accountid` = a.`memberid` 
       LEFT JOIN `table3` d 
              ON a.`id` = d.`memberid`
WHERE  c.`is_active` = 1 
       AND a.memberid IS NULL 
       AND b.accountid IS NULL 
       AND DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= d.datecreated
ORDER  BY uid 

```

records:

```
tbl_members
---------------------------
id | username | is_active |
 1 |   user1  |     1     |
---------------------------
table1
------------------------------
id | accountid | datecreated |
   |           |             |
------------------------------
table2
------------------------------
id | memberid  | datecreated |
   |           |             |
------------------------------
table3
----------------------------------------
id | memberid  |      datecreated      |
 1 |     1     | 2018-06-21 00:12:51   |
----------------------------------------

```

when I remove this line: 

```
AND DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= d.datecreated

```

I'm getting the right results.

EDIT:
I'm getting the right results now by using this query:

```
SELECT b.accountid, 
       a.memberid, 
       d.memberid, 
       c.memberid AS uid, 
       d.datecreated 
FROM   `tbl_members` c 
       LEFT JOIN `tbl_bonus_direct` b 
              ON c.`memberid` = b.`accountid` 
       LEFT JOIN `tbl_wallet_cash` a 
              ON b.`accountid` = a.`memberid` 
       LEFT JOIN `tbl_members_accounts` d 
              ON c.`memberid` = d.`memberid`
WHERE  c.`is_active` = 1 
       AND a.memberid IS NULL 
       AND b.accountid IS NULL 
       AND DATE_SUB(CURDATE(), INTERVAL 30 DAY) >= date(d.datecreated)
ORDER  BY uid 

```

BUT I want to distinct the memberid in table3