1

我的桌子:

feeds_time:

user_id | time
1       | 123123
2       | 1231111
3       | 1233223
...

users_follow

user_id | follow_id
1       | 2
1       | 3
2       | 3

和表格帖子 - 但这在这个问题中并不重要。

实际上我的查询看起来像:

SELECT `id`, `name`, `user_id`, `time`, 'posts' as `what`
FROM `posts`
WHERE `user_id` IN (SELECT `follow_id`
                    FROM users_follow WHERE user_id = posts.user_id)

现在,我想添加一个 WHERE: WHERE posts.time > feeds_time.timeby "current" user_id

我该怎么做?

4

3 回答 3

1

尝试这个:

    SELECT * FROM (SELECT `id`, `name`, `user_id`, `time`, 'posts' as `what`
    FROM `posts`
    WHERE `user_id` IN (SELECT `follow_id` FROM users_follow 
    WHERE user_id = posts.user_id)) a, feeds_time 
    where a.`time` > feeds_time.time and a.`user_id` = feeds_time.`user_id`
于 2013-10-23T18:16:50.523 回答
1

你可以试试这个

使用连接

SELECT A.`id`, A.`name`, A.`user_id`, A.`time`, A.'posts' as `what`
FROM `posts` A
JOIN feeds_time B ON A.user_id = B.user_id
WHERE A.`user_id` IN (SELECT `follow_id`
                    FROM users_follow WHERE user_id = posts.user_id)
AND A.time > B.time
于 2013-10-23T18:22:28.230 回答
0

你可以试试这个查询:

SELECT a.* 
FROM feeds_time a 
INNER JOIN users_follow b
    ON a.user_id = b.user_id  
INNER JOIN feeds_time c
    ON b.user_id = c.user_id  
    AND a.time > b.time 
于 2013-10-23T18:20:59.017 回答