To get all records from first table you need to use
LEFT JOIN
statement.
SELECT t1.id AS referred_by_user_id, COUNT(t2.referred_by_user_id) AS [got]
FROM Table1 AS t1 LEFT JOIN Table2 AS t2 ON t1.id = t2.referred_by_user_id
GROUP BY t1.id
Check date from first table not the second table. - i don't get it ;(
Does it mean you want to get only those
referred_by_user_id
's from first table which date is equal to second table for that particular
id
? When you explain it, i'll improve my answer.
[EDIT #1]
Sorry, i forgot about grouping options ;(
Now, result:
ref. got
522 2
523 0
524 0
525 0
584 5
590 1
[EDIT]