人们可以在我的网站上发表喜欢的文章。
我想使用他们发表的文章数量和收到的喜欢数量计算每个成员的等级。
我有一个要求很好的请求,但由于子查询,一次只能供一个成员使用。因此,目前,我只能在他们的个人资料页面上进行操作。
我想要求所有成员为我的首页设置前3名。
您对我该如何解决有任何想法?我是开发中的新手,所以我想我的代码已经不太好了。感谢您的回答和建议。
// this code for one person, works fine
SELECT
(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 row
SELECT
(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 Article in first - and then table for like. They are quite simple I guess.
“喜欢”表非常简单,人们只能喜欢或删除他们的喜欢,他们不能不喜欢。 表Like和Article之间的链接由idArticle建立。 我也有一个供成员使用的表,但是在这里没有使用它。
答案 0 :(得分:1)
这有点脏,但是您可以这样实现。假设您需要某篇文章拥有喜欢,您可以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
答案 1 :(得分:0)
您可以通过伪列加入suqbeury
select t1.pseudo,t1.nbLikes, t2.nbArticle, t1.nbLikes + t2.nbArticle,
from (
select a.pseudo, count(l.idArticle) as nbLikes
from likes as l
inner join articles as a on l.idArticle = a.idArticle
group by a.pseudo
) t1
left join (
select pseudo, count(pseudo) as nbArticle
from articles
group by pseudo
) t2 ON t1.pseudo = t2.pseudo