我有两个表,赞和评论,这两个表都引用了主题帖子 - 例如论坛的主题。
它们都有一个引用特定topic_id的列。
现在这里有了这笔交易:我想创建一个最受喜爱+评论的前5名图表,这两者的总和我的意思是主题。
例如,我做了这个查询,从主题表中选择最喜欢的,我想制作相同的图表,总共喜欢+评论。这是我最喜欢的前5个主题。
SELECT topics.* ,
COUNT(q_id)
AS post_count
FROM topics
LEFT JOIN likes
ON topics.id = likes.q_id
WHERE topics.to_user = 'someuser'
GROUP BY likes.q_id
ORDER BY post_count DESC
LIMIT 0, 5
提前Tnx!
答案 0 :(得分:0)
这样的事情应该有效:
SELECT likes.topic_id, comments.topic_id FROM likes,comments WHERE comments.topic_id = likes.topic_id AND likes.topic_id=[topicid]
这将只返回id匹配的行。那你就算数行。在php中你可以使用:mysql_num_rows()
答案 1 :(得分:0)
这就像一个魅力!!
var datePicker: UIDatePicker!
答案 2 :(得分:-1)
SELECT posts.id, count(comments.id) + count(likes.id) AS score
FROM posts
LEFT JOIN comments ON posts.id = comments.post_id
LEFT JOIN likes ON posts.id = likes.post_id
GROUP BY posts.id
ORDER BY score desc;
对于那些感兴趣的人来说,解决方案。