Click here to Skip to main content
15,907,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to count all user id of the first table that don't have refer id in the second table?

Table 1:
+-----+------------+---------------------+
| id  | first_name | created_at          |
+-----+------------+---------------------+
| 522 | dddd       | 2013-04-11 18:44:07 |
| 523 | cccc       | 2013-03-01 06:19:12 |
| 524 | ffff       | 2013-03-06 21:07:06 |
| 525 | rrrr       | 2013-03-06 22:05:08 |
| 590 | tttt       | 2013-03-16 16:39:59 |
| 584 | yyyy       | 2013-03-22 14:46:28 |
 
Table 2:
+-----+---------------------+---------------------+
| id  | referred_by_user_id | created_at          |
+-----+---------------------+---------------------+
| 298 |                 522 | 2013-02-22 22:19:06 |
| 299 |                 584 | 2013-03-22 18:27:49 |
| 300 |                 584 | 2013-03-22 18:48:23 |
| 301 |                 584 | 2013-03-22 19:01:40 |
| 302 |                 584 | 2013-03-22 19:05:24 |
| 303 |                 584 | 2013-03-22 19:13:18 |
| 304 |                 590 | 2013-04-10 23:24:17 |
| 305 |                 522 | 2013-04-11 18:44:07 |
Posted

1 solution

Try:
SQL
SELECT COUNT(*) FROM Table1 t1 
LEFT JOIN Table2 t2 ON t1.id=t2.referred_by_user_id 
WHERE t2.referred_by_user_id IS NULL
 
Share this answer
 
Comments
Yafa Su 22-Apr-13 15:49pm    
Thank it works, but if i replace 'is null' to '= 4' it's not working you know why?
OriginalGriff 22-Apr-13 16:14pm    
:laugh: Yes!
It has to do with what a left join is: and W3Schools.com can explain it better than I can.
StianSandberg 22-Apr-13 17:21pm    
Perfect. 5'd

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900