我有一个页面,人们可以发布评论和页面,人们可以点击“关注”其他人的个人资料(与Facebook上的LIKE相同)
我想让SELECT查询发布我所有的评论,但会按照以下方式对它们进行排序: 首先,打印您点击FOLLOW的最新评论的2条最新评论(本周必须发布)。 第二,发布其余的帖子,按创建日期
订购(我正在使用linux时间)
你能帮我解决SQL查询吗?
这是我当前的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
“粉丝”表格如下:
userID ownerID createDate
1 2 1439019657
1 4 1438940399
“评论”表看起来很喜欢这个:
id userID pageName refID text createDate
220 1 yard 0 text1 1438030967
227 1 yard 0 text2 1438031704
228 1 yard 0 text3 1438031704
答案 0 :(得分:0)
我可能搞砸了追随者的关系。示例数据看起来完全适用于完整上下文。它肯定显示userID = 1条评论,但是没有人跟随userID = 1或者3和4都跟着他。不知道refID在评论表中做了什么。查询只显示一个概念 - 你会弄清楚细节。
SELECT un.* FROM ((
SELECT 1 as priority, comment.id, comment.userID, comment.text, comment.createDate
FROM `comments` as comment
INNER JOIN `followers` as follower ON follower.userID = comment.userID
WHERE comment.refID = 0
AND comment.pageName = 'yard'
AND ...
ORDER BY follower.createDate DESC LIMIT 2
) UNION DISTINCT (
SELECT 2 as priority, id, userID, text, createDate
FROM `comments` as comment
WHERE comment.refID = 0
AND comment.pageName = 'yard'
)) as un ORDER BY un.priority, un.createDate DESC LIMIT 20