人们可以在我的网站上发表喜欢的文章。我想使用他们发表的文章数量和收到的喜欢次数来计算每个成员的等级。我有一个要求很好的请求,但由于子查询,一次只能供一个成员使用。因此,目前,我只能在他们的个人资料页面上进行操作。我希望所有成员都可以为我的首页设置前3名。您对我该如何解决有任何想法?我是开发中的新手,所以我想我的代码已经不太好了。感谢您的回答和建议。// this code for one person, works fineSELECT (select (count(l.idArticle)) as nbLikes from likes as l inner join articles as a on l.idArticle = a.idArticle where a.pseudo ="' . $_SESSION['pseudo']. '") + ( select count(pseudo) as nbArticle from articles and pseudo ="' . $_SESSION['pseudo']. '") as sumCount// code i tried for all member but did not work, saying subqueries have more than one rowSELECT (select (count(l.idArticle)/3) as nbLikes from likes as l inner join articles as a on l.idArticle = a.idArticle group by a.pseudo) + ( select count(pseudo) as nbArticle from articles group by pseudo) as sumCount#1242-子查询返回的行数超过1表结构(对不起,每列的名称用法语表示): 首先是表文章-然后是类似表。我猜它们很简单。Table for Like很简单,人们只能喜欢或删除他们的喜欢,他们不能不喜欢。表Like和Article之间的链接是通过idArticle建立的。我也有一个供成员使用的表,但是这里没有使用它。
2 回答
侃侃无极
TA贡献2051条经验 获得超10个赞
这有点脏,但是您可以这样实现。假设您需要一篇文章来拥有喜欢,您可以LEFT JOIN将伪名称中的喜欢结果(按伪名称分组)。IFNULL()如果给定帖子没有喜欢,请使用。
SELECT a.pseudo,
a.ArticleCount,
IFNULL(likes, 0) as LikesCount,
IFNULL(likes, 0)/3 + ArticleCount as Score
FROM (
SELECT IFNULL(COUNT(a.idArticle), 0) as ArticleCount, a.pseudo
FROM articles a
GROUP BY a.pseudo
) as a
-- Find all likes that user has gotten
LEFT JOIN (
SELECT COUNT(l.idLike) as likes, a.pseudo
FROM likes l
JOIN articles a
ON a.idArticle = l.idArticle
GROUP BY a.pseudo
) as l
on a.pseudo=l.pseudo
SQLFiddle显示正在运行的查询:http ://sqlfiddle.com/#!9/375b6f/22
- 2 回答
- 0 关注
- 156 浏览
添加回答
举报
0/150
提交
取消