gpt4 book ai didi

php - 我希望我的子查询返回多个值(= 为每个成员),这可能吗?

转载 作者:行者123 更新时间:2023-11-29 07:21:47 25 4
gpt4 key购买 nike

在我的网站上,人们可以发布文章并点赞。

我想根据他们发表的文章数量和收到的点赞数量来计算每个成员的等级。

我有一个工作正常的请求,但我猜是因为子查询,一次只能为一个成员工作。所以目前,我只能在他们的个人资料页面上进行。

我想请求所有成员为我的主页做一个前 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 - Subquery returns more than 1 row

表结构(抱歉,每一列的名称都是法语): Table Article in first - and then table for like. They are quite simple I guess.

点赞表非常简单,人们只能点赞或删除自己的点赞,不能点赞。表格 Like 和 Article 之间的链接是使用 idArticle 建立的。我也有一张成员(member) table ,但没有在这里使用。

最佳答案

有点脏,但你可以这样实现。鉴于您需要一篇文章才能获得点赞,您可以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

关于php - 我希望我的子查询返回多个值(= 为每个成员),这可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55967182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com