mysql聚合困难

发布于 2024-10-31 12:14:24 字数 1538 浏览 0 评论 0原文

我的数据库中有两个表结构:

CREATE TABLE `projects` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(32) DEFAULT NULL,
 `description` varchar(128) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8

CREATE TABLE `issues` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `member_id` varchar(10) DEFAULT NULL,
 `project_id` int(11) DEFAULT NULL,
 `name` varchar(32) DEFAULT NULL,
 `description` varchar(128) DEFAULT NULL,
 `date_created` date DEFAULT NULL,
 `type` enum('general','bug','requirement') DEFAULT NULL,
 `priority` enum('low','medium','high') DEFAULT NULL,
 `status` enum('resolved','open','discarded') DEFAULT NULL,
 PRIMARY KEY (`id`),
 KEY `member_id` (`member_id`),
 KEY `project_id` (`project_id`)
) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=utf8

我想要做的是创建一个选择语句,该语句返回项目名称以及与该项目相关的已解决问题的数量。我创建了以下 SQL 语句:

select projects.name, count(*) from projects left join issues on projects.id = issues.project_id where status = 'resolved' group by projects.name

但是,这仅返回至少有 1 个已解决问题的项目,我需要它也返回有 0 个已解决问题的项目。

我已经有一段时间没有做mySQL了,有人可以帮忙吗?谢谢。


我想我应该提供更多信息,因为我还没有收到有效的答案。如果我有四个项目,每个项目有一个问题,其中两个问题已“解决”,我希望查询返回:

project_name    | count(*)
--------------------------
first_project   | 1
second_project  | 0
third_project   | 0
fourth_project  | 1

但是,查询仅返回至少有一个已解决问题的项目。

project_name   | count(*)
-------------------------
first_project  | 1
fourth_project | 1

I have two table structures in my database:

CREATE TABLE `projects` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(32) DEFAULT NULL,
 `description` varchar(128) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8

CREATE TABLE `issues` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `member_id` varchar(10) DEFAULT NULL,
 `project_id` int(11) DEFAULT NULL,
 `name` varchar(32) DEFAULT NULL,
 `description` varchar(128) DEFAULT NULL,
 `date_created` date DEFAULT NULL,
 `type` enum('general','bug','requirement') DEFAULT NULL,
 `priority` enum('low','medium','high') DEFAULT NULL,
 `status` enum('resolved','open','discarded') DEFAULT NULL,
 PRIMARY KEY (`id`),
 KEY `member_id` (`member_id`),
 KEY `project_id` (`project_id`)
) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=utf8

What I am trying to do is create a select statement that returns the project name, and the number of resolved issues that are associated with that project. I have created the following SQL statement:

select projects.name, count(*) from projects left join issues on projects.id = issues.project_id where status = 'resolved' group by projects.name

However this only returns projects that have at least one resolved issue, I need it to return projects that have 0 resolved issues as well.

It's been a while since I have done any mySQL, can anyone help out? Thanks.


I thought I'd give some more information since I haven't received a working answer yet. If I had four projects and one issue for each project, with two of those issues being 'resolved', I'd expect the query to return:

project_name    | count(*)
--------------------------
first_project   | 1
second_project  | 0
third_project   | 0
fourth_project  | 1

However, the query is only returning projects that have at least one resolved issue.

project_name   | count(*)
-------------------------
first_project  | 1
fourth_project | 1

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

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

发布评论

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

评论(1

鹤舞 2024-11-07 12:14:24

将您的查询更改为:

   SELECT p.name, count(i.*)
     FROM PROJECTS p    
LEFT JOIN ISSUES i ON p.id = i.project_id 
                  AND i.status = 'resolved'
 GROUP BY projects.name

在 where 子句中包含“status = 'resolved'”导致它只返回那些至少有一个问题的项目

change your query to:

   SELECT p.name, count(i.*)
     FROM PROJECTS p    
LEFT JOIN ISSUES i ON p.id = i.project_id 
                  AND i.status = 'resolved'
 GROUP BY projects.name

having "status = 'resolved'" in the where clause caused it only to return those projects with at least one issue

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