Below is my database structure.
users
id|username|email |country|datenumber|
--|--------|----- |-------|----------|
1|user1 |example@example.com | GB |2017-11-08|
2|user2 |example@example.com | GB |2017-11-08|
3|user3 |example@example.com | GB |2017-11-08|
4|user4 |example@example.com | GB |2017-11-08|
5|user5 |example@example.com | GB |2017-11-08|
faillogs
id|userid |type |datenumber|
--|--------|------------|----------|
1|1 |wronganswer |2017-11-08|
2|3 |wronganswer |2017-11-08|
3|3 |wronganswer |2017-11-08|
4|3 |wronganswer |2017-11-08|
5|5 |wronganswer |2017-11-08|
6|5 |wronganswer |2017-11-08|
7|5 |wronganswer |2017-11-08|
8|5 |wronganswer |2017-11-08|
9|5 |wronganswer |2017-11-08|
cashouts
id|userid |amount |datenumber|
--|--------|------------|----------|
1|1 |1.47 |2017-11-08|
1|1 |2.97 |2017-11-08|
1|2 |1.05 |2017-11-08|
My question is asking, how I can fetch all the users which have joined but also list a count of faillogs.
The below SQL fetches all the users
SELECT * FROM users
The SQL below does a join to fetch the faillogs.
SELECT *, COUNT(faillogs.userid) FROM users
INNER JOIN faillogs
ON users.id=faillogs.userid
GROUP BY faillogs.userid
ORDER BY users.datenumber ASC
I would like the benefits of being able to fetch a count of the faillogs for each user with a join WITH the added benefit of showing all users, not just the users that have a faillog. How do I do this?
I have a SQL fiddle here. http://sqlfiddle.com/#!9/56cf4/4