MySQL select 对另一个表进行计数查询

发布于 2024-12-09 00:28:13 字数 1427 浏览 0 评论 0原文

我有一个包含三个表的简单文章应用程序:

article 
id, title,  body,   user_id

comment
id, article_id, user_id,    body

user
id, username

在登陆页面上,我想显示最新的文章标题以及作者姓名和文章的评论总数。主要问题是如何获取文章的评论总数,我没搞对。我应该得到以下输出:

title           username    total_comments
article 2       user2           0
article 1       user1           2

在我的实际应用程序中,我在文章表中添加了一列,用于显示文章的评论总数。当系统中添加新评论时,此列会更新。这样做的问题是,当添加新评论时,文章表被锁定。在我的应用程序中,每分钟都会添加很多评论。所以我试图通过重写 SQL 查询来避免锁定文章表。

以下是一些测试数据:

CREATE TABLE `article` (
`id` INT NULL AUTO_INCREMENT PRIMARY KEY ,
`title` VARCHAR( 100 ) NULL ,
`body` LONGTEXT NULL ,
`user_id` INT NULL
) ENGINE = MYISAM ;


CREATE TABLE `comment` (
`id` INT NULL AUTO_INCREMENT PRIMARY KEY ,
`article_id` INT NULL ,
`user_id` INT NULL ,
`body` LONGTEXT NULL
) ENGINE = MYISAM ;

CREATE TABLE `user` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`username` VARCHAR( 50 ) NOT NULL
) ENGINE = MYISAM ;


INSERT INTO `test`.`user` (
`id` ,
`username`
)
VALUES (
NULL , 'user1'
), (
NULL , 'user2'
);


INSERT INTO `test`.`article` (
`id` ,
`title` ,
`body` ,
`user_id`
)
VALUES (
NULL , 'article 1', 'body article 1', '1'
), (
NULL , 'article 2', 'body article 2', '2'
);

INSERT INTO `test`.`comment` (
`id` ,
`article_id` ,
`user_id` ,
`body`
)
VALUES (
NULL , '1', '1', 'body comment to article 1'
), (
NULL , '1', '1', 'body comment to article 1'
);

I have simple article application with three tables:

article 
id, title,  body,   user_id

comment
id, article_id, user_id,    body

user
id, username

On the landing page, I want to show the latest article titles with the author name and total numbers of comments of the article. The main problem is how to get total numbers of comments of the article,I did not get it right. I should get the following output:

title           username    total_comments
article 2       user2           0
article 1       user1           2

In my real application, I added a column in article table for total number of comments to the article. this column is updated when a new comment is added to the system. The problem with this is that the article table is locked when a new comment is added. In my application a lot of comments are added every minutes. So I am trying to avoid locking article table with re-writing the SQL query.

Here is some data for testing:

CREATE TABLE `article` (
`id` INT NULL AUTO_INCREMENT PRIMARY KEY ,
`title` VARCHAR( 100 ) NULL ,
`body` LONGTEXT NULL ,
`user_id` INT NULL
) ENGINE = MYISAM ;


CREATE TABLE `comment` (
`id` INT NULL AUTO_INCREMENT PRIMARY KEY ,
`article_id` INT NULL ,
`user_id` INT NULL ,
`body` LONGTEXT NULL
) ENGINE = MYISAM ;

CREATE TABLE `user` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`username` VARCHAR( 50 ) NOT NULL
) ENGINE = MYISAM ;


INSERT INTO `test`.`user` (
`id` ,
`username`
)
VALUES (
NULL , 'user1'
), (
NULL , 'user2'
);


INSERT INTO `test`.`article` (
`id` ,
`title` ,
`body` ,
`user_id`
)
VALUES (
NULL , 'article 1', 'body article 1', '1'
), (
NULL , 'article 2', 'body article 2', '2'
);

INSERT INTO `test`.`comment` (
`id` ,
`article_id` ,
`user_id` ,
`body`
)
VALUES (
NULL , '1', '1', 'body comment to article 1'
), (
NULL , '1', '1', 'body comment to article 1'
);

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

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

发布评论

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

评论(4

娜些时光,永不杰束 2024-12-16 00:28:14

像这样的事情怎么样,现在无法测试,所以可能会有错误。

SELECT a.title, u.username, COUNT(*) total_comments
FROM article a
  JOIN user u ON (u.id=a.user_id)
  LEFT OUTER JOIN comment c ON (c.article_id=a.id)
GROUP BY a.title, u.username

How about something like this, can't test it right now so it might have errors.

SELECT a.title, u.username, COUNT(*) total_comments
FROM article a
  JOIN user u ON (u.id=a.user_id)
  LEFT OUTER JOIN comment c ON (c.article_id=a.id)
GROUP BY a.title, u.username
早乙女 2024-12-16 00:28:14
SELECT article.title AS title, COUNT(  'comment.id' ) AS total_comment, user.username AS username
FROM article
JOIN COMMENT ON comment.article_id = article.id
JOIN user ON user.id = article.user_id
GROUP BY article.id
SELECT article.title AS title, COUNT(  'comment.id' ) AS total_comment, user.username AS username
FROM article
JOIN COMMENT ON comment.article_id = article.id
JOIN user ON user.id = article.user_id
GROUP BY article.id
陪我终i 2024-12-16 00:28:14

如果你尝试这个,它会回答吗?

SELECT a.title AS title, 
             u.username AS username, 
             count(c.id) AS total_comments 
 FROM articles a, comments c, users u
 WHERE  a.user_id =u.id
    And a.id=c.articles_id
    And c.user_id = u.id 

或者使用内部语法

SELECT a.title AS title, 
             u.username AS username, 
             count(c.id) AS total_comments 
 FROM articles a 
 INNER JOIN comments c ON c.article_id = a.id 
                                          AND c.user_id=u.id
 INNER JOIN users u ON a.user_id = u.id 
 GROUP BY a.id

If you try this that it answer?

SELECT a.title AS title, 
             u.username AS username, 
             count(c.id) AS total_comments 
 FROM articles a, comments c, users u
 WHERE  a.user_id =u.id
    And a.id=c.articles_id
    And c.user_id = u.id 

Or with inner syntax

SELECT a.title AS title, 
             u.username AS username, 
             count(c.id) AS total_comments 
 FROM articles a 
 INNER JOIN comments c ON c.article_id = a.id 
                                          AND c.user_id=u.id
 INNER JOIN users u ON a.user_id = u.id 
 GROUP BY a.id
女皇必胜 2024-12-16 00:28:13
SELECT a.title AS title, u.username AS username, count(c.id) as total_comments FROM articles a
   LEFT JOIN comments c ON c.article_id = a.id
   LEFT JOIN users u ON a.user_id = u.id
GROUP BY a.id
SELECT a.title AS title, u.username AS username, count(c.id) as total_comments FROM articles a
   LEFT JOIN comments c ON c.article_id = a.id
   LEFT JOIN users u ON a.user_id = u.id
GROUP BY a.id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文