mysql聚合困难
我的数据库中有两个表结构:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将您的查询更改为:
在 where 子句中包含“status = 'resolved'”导致它只返回那些至少有一个问题的项目
change your query to:
having "status = 'resolved'" in the where clause caused it only to return those projects with at least one issue