MySQL COUNT 函数在多个连接查询中没有按照我想要的方式执行

发布于 2024-12-02 00:01:15 字数 1269 浏览 1 评论 0原文

我已经改变查询有一段时间了,阅读了这个精彩网站上的许多帖子,以了解到目前为止我的情况。但是,唉,现在我陷入困境了:(

这个查询和我设计的数据库的相关部分类似于youtube评论喜欢系统。相关表和字段是:

USRS
usr_id int (PK)

评论
comment_id int (PK) usr_id int (FK) 引用 usr(usr_id) topic_id int (FK) 引用主题(topic_id) 描述 varchar 创建了 varchar

COMMENT_LIKERS
comment_id int (PK) (FK) 引用评论(comment_id) usr_id int (PK) (FK) 引用 usr(usr_id) liker tinyint

我希望能够在一个查询中选择所有相关数据。除了每个评论的一般数据之外,我还想计算每个评论的所有喜欢和不喜欢。到目前为止,我的查询正在计算所有评论的点赞数,而不是每个评论的点赞数,即使我有 LEFT JOIN with ON 子句:comments.comment_id = comment_likers.comment_id。

我正在学习 MySql 和 PHP,所以如果我做了一些愚蠢的事情,请放轻松。我向你保证,我已经到处寻找答案的线索。

这是查询:

SELECT comments.comment_id, comments.descr, comments.created, usrs.usr_name,
  COUNT(if(comment_likers.liker = 1, 1, null)),
  COUNT(if(comment_likers.liker = 0, 1, null)),
  comment_likers2.liker
FROM comments
INNER JOIN usrs ON ( comments.usr_id = usrs.usr_id )
LEFT JOIN comment_likers ON ( comments.comment_id = comment_likers.comment_id )
LEFT JOIN comment_likers AS comment_likers2 ON ( comments.comment_id = comment_likers.comment_id AND comment_likers.usr_id = $usrID )
WHERE comments.topic_id = $tpcID
GROUP BY comments.comment_id
ORDER BY comments.created DESC

提前致谢

I have been altering a query for a while now, reading many posts on this wonderful site to get to where I am with this so far. But, alas, now I am stuck :(

This query and relevant part of the database I have designed is similar to the youtube comment liking system. Relevant tables and fields are:

USRS
usr_id int (PK)

COMMENTS
comment_id int (PK)
usr_id int (FK) references usrs(usr_id)
topic_id int (FK) references topics(topic_id)
descr varchar
created varchar

COMMENT_LIKERS
comment_id int (PK) (FK) references comments(comment_id)
usr_id int (PK) (FK) references usrs(usr_id)
liker tinyint

I want to be able to select all relevant data in one query. Aside from general data for each comment, I want to count all likes and dislikes for each comment. The query I have thus far is counting the likes for ALL comments and not for each even though I have the LEFT JOIN with ON clause: comments.comment_id = comment_likers.comment_id.

I am learning MySql and PHP so go easy with me if I have done something silly. I assure you I have looked all around for clues to the answer.

Here is the query:

SELECT comments.comment_id, comments.descr, comments.created, usrs.usr_name,
  COUNT(if(comment_likers.liker = 1, 1, null)),
  COUNT(if(comment_likers.liker = 0, 1, null)),
  comment_likers2.liker
FROM comments
INNER JOIN usrs ON ( comments.usr_id = usrs.usr_id )
LEFT JOIN comment_likers ON ( comments.comment_id = comment_likers.comment_id )
LEFT JOIN comment_likers AS comment_likers2 ON ( comments.comment_id = comment_likers.comment_id AND comment_likers.usr_id = $usrID )
WHERE comments.topic_id = $tpcID
GROUP BY comments.comment_id
ORDER BY comments.created DESC

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左秋 2024-12-09 00:01:15
SELECT comments.comment_id, comments.descr, comments.created, usrs.usr_name, 
  (SELECT COUNT(*) FROM comment_likers WHERE comment_id=comments.comment_id AND liker=1)likes,
  (SELECT COUNT(*) FROM comment_likers WHERE comment_id=comments.comment_id AND liker=0)dislikes
  liker
FROM comments
INNER JOIN usrs ON ( comments.usr_id = usrs.usr_id )
LEFT JOIN comment_likers  ON ( comments.comment_id = comment_likers.comment_id 
 AND comment_likers.usr_id = $usrID )
WHERE comments.topic_id=$tpcID
ORDER BY comments.created DESC;

一些笔记。我不太确定 comment_likers 上的第二个左连接应该完成什么(使用 $usrID 的连接)。您是否只对特定用户对特定主题的点赞感兴趣?

另外,您可能会考虑将创建的注释的架构更改为日期时间而不是 varchar。

SELECT comments.comment_id, comments.descr, comments.created, usrs.usr_name, 
  (SELECT COUNT(*) FROM comment_likers WHERE comment_id=comments.comment_id AND liker=1)likes,
  (SELECT COUNT(*) FROM comment_likers WHERE comment_id=comments.comment_id AND liker=0)dislikes
  liker
FROM comments
INNER JOIN usrs ON ( comments.usr_id = usrs.usr_id )
LEFT JOIN comment_likers  ON ( comments.comment_id = comment_likers.comment_id 
 AND comment_likers.usr_id = $usrID )
WHERE comments.topic_id=$tpcID
ORDER BY comments.created DESC;

A couple notes. I wasn't too sure what the second left join on comment_likers was supposed to accomplish (the one using $usrID). Are you only interested in likes from on a specific topic from a specific user?

Also, you might think about changing the schema for comments created to be a datetime instead of a varchar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文