我有一个页面,人们可以在其中发布comments
和人们点击的页面" follow
"在其他人资料上(与LIKE
上的Facebook
相同)
我希望SELECT
查询会发布我的所有评论,但会按照以下方式对其进行排序:
首先,打印您点击comments
的{{1}}人中的最新week
(必须已发布此lastest
)。
其次,发布其余帖子,FOLLOW
order
(我使用create-date
)
你能帮我解决SQL查询吗?
这是我当前的linux time
查询。它通过SELECT
提取所有评论:
create-date
" SELECT
id, userID, text, createDate
FROM `comments` AS comment
WHERE (comment.refID = 0)
AND (comment.pageName = 'yard')
AND 1=1
ORDER BY comment.createDate DESC LIMIT 0, 20
"表看起来像这样:
followers
(用户1在用户2和4之后关注)
" userID ownerID createDate
1 2 1439019657
1 4 1438940399
"表看起来喜欢这个:
comments
( id userID pageName refID text createDate
220 1 yard 0 text1 1438030967
227 1 yard 0 text2 1438031704
228 1 yard 0 text3 1438031704
- 哪位用户发布评论。userID
- 始终" refID
"。0
- 始终" {{ 1}}&#34)
在这种情况下,如果我是用户编号1,那么我希望看到用户2和4的最新2条评论(仅当它们在最后pageName
中所做的那样)而不是查看所有其他评论(yard
)week
(当然,没有我曾经看过的那篇)
答案 0 :(得分:0)
您将不得不将其拆分为2个查询.. ..我猜这里有一些表关系..
// get the last 2 comments from followed users..
SELECT *
FROM comments
WHERE userID IN (
SELECT ownerID
FROM followers
WHERE userID = ?
) ORDER BY createDate DESC
LIMIT 2
// then get the rest, removing already returned comments
SELECT *
FROM comments
WHERE id NOT IN (
-- either put the above query in here
-- or buuild an array of the 'id's when outputting the results above
-- and use that in here to prevent them being returned again
) ORDER BY createDate DESC
答案 1 :(得分:0)
(select com.userID,com.page,com.text
from followers as fol
JOIN comments as com
ON fol.ownerId=com.userId
where
(
from_unixtime(com.createDate) BETWEEN
(
UNIX_TIMESTAMP(DATE_ADD(CURDATE(),INTERVAL -14 DAY) AND UNIX_TIMESTAMP(NOW())
)
Order BY com.createDate desc
)
Union
(select com.userID,com.page,com.text
from comments as com
where com.id NOT In
(select com.id
from followers as fol
JOIN comments as com
ON fol.userId=com.userId
where
(
from_unixtime(com.createDate) BETWEEN
(
UNIX_TIMESTAMP(DATE_ADD(CURDATE(),INTERVAL -14 DAY) AND UNIX_TIMESTAMP(NOW())
)
)
Order BY com.createDate desc
)
<强>说明:强> 有两个查询合并在一个使用union。
唯一不明确的是你是如何决定的 评论已由用户阅读(信息不足)