I have a database with some tables and I want to retrieve from a user the last 8 tee of user that I follow:
This is my table:
Table users:
- id
- name
- surname
- created
- modified
2 | Caesar | Surname1
3 | Albert | Surname2
4 | Paul | Surname3
5 | Nicol | Surname4
Table tee
- id
- name
- user_id
1 | first | 3
2 | second | 3
3 | third | 4
4 | fourth | 4
5 | fifth | 5
6 | sixth | 5
7 | seventh | 5
table user_follow
- id
- user_follower_id //the user that decide to follo someone
- user_followed_id //the user that I decide to follow
1 | 2 | 3
2 | 2 | 5
I expect to retrieve this tee with its creator because my id is 2 (I'm Caesar for example):
1 | first | 3
2 | second | 3
5 | fifth | 5
6 | sixth | 5
7 | seventh | 5
For example if I user that I follow have created 4 tee another that I follow 1, another 2, I think that I can retrieve all this tee if are the last inserted in all sites because are created from user that I follow.
But I retrieve only one tee of an user
This is my query:
SELECT *, `tee`.`id` as id, `tee`.`created` as created, `users`.`id` as user_id, `users`.`created` as user_created
FROM (`tee`)
LEFT JOIN `users`
ON `users`.`id` = `tee`.`user_id`
LEFT JOIN `user_follow` ON `tee`.`user_id` = `user_follow`.`user_followed_id`
WHERE `tee`.`id` != '41' AND
`tee`.`id` != '11' AND
`tee`.`id` != '13' AND
`tee`.`id` != '20' AND
`tee`.`id` != '14' AND
`tee`.`id` != '35' AND
`tee`.`id` != '31' AND
`tee`.`id` != '36' AND
`user_follow`.`user_follower_id` = '2'
ORDER BY `tee`.`created` desc LIMIT 8
I have added 8 tee id that I don't want to retrieve because are "special".
Why this query doesn't work?
I think the problem is in left join or I have to make other thing to retreve this results.
Thanks