1

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

4

3 回答 3

1

我看不出您的查询有什么问题——我已经更新了使用INNER JOINs和使用的语法NOT IN

SELECT *, 
    `tee`.`id` as id, `tee`.`created` as created, `users`.`id` as user_id, `users`.`created` as user_created 
FROM `tee`
    JOIN `users` ON `users`.`id` = `tee`.`user_id` 
    JOIN `user_follow` ON `tee`.`user_id` = `user_follow`.`user_followed_id` 
WHERE `tee`.`id` NOT IN (41,11,13,20,14,35,31,36) 
    AND `user_follow`.`user_follower_id` = '2' 
ORDER BY `tee`.`created` desc 
LIMIT 8
于 2013-07-16T13:16:42.063 回答
0

使用 UNION 子句。首先选择您的 T 恤,然后从您关注的人中选择 T 恤。

于 2013-07-16T13:04:50.650 回答
0

您可以使用 Not In ('41','11',...) 代替 tee. id!= '41' 和tee. id!= '11'......

于 2013-07-16T12:57:22.770 回答