我有三张空表
--
-- Tabellenstruktur für Tabelle `projects`
--
CREATE TABLE IF NOT EXISTS `projects` (
`id_project` int(11) NOT NULL AUTO_INCREMENT,
`id_plan` int(11) DEFAULT NULL,
`name` varchar(255) NOT NULL,
`description` longtext NOT NULL,
PRIMARY KEY (`id_project`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
-- --------------------------------------------------------
--
-- Tabellenstruktur für Tabelle `project_plans`
--
CREATE TABLE IF NOT EXISTS `project_plans` (
`id_plan` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`description` longtext NOT NULL,
`max_projects` int(11) DEFAULT NULL,
`max_member` int(11) DEFAULT NULL,
`max_filestorage` bigint(20) NOT NULL DEFAULT '3221225472' COMMENT '3GB Speicherplatz',
PRIMARY KEY (`id_plan`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
-- --------------------------------------------------------
--
-- Tabellenstruktur für Tabelle `project_users`
--
CREATE TABLE IF NOT EXISTS `project_users` (
`id_user` int(11) NOT NULL,
`id_project` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
所有这些表都是空的,但我的查询得到了结果? 我的疑问:
SELECT
A.id_plan,
A.name AS plan_name,
A.description AS plan_description,
A.max_projects,
A.max_member,
A.max_filestorage,
B.id_plan,
B.name AS project_name,
B.description AS project_description,
C.id_user,
C.id_project,
COUNT(*) AS max_project_member
FROM
".$this->config_vars["projects_plans_table"]." AS A
LEFT JOIN
".$this->config_vars["projects_table"]." AS B
ON
B.id_plan = A.id_plan
LEFT JOIN
".$this->config_vars["projects_user_table"]." AS C
ON
C.id_project = B.id_project
WHERE
C.id_project = '".$id."'
&& B.deleted = '0'
我认为问题是COUNT(*)AS ...
我怎么能解决问题?
答案 0 :(得分:1)
首先,由于COUNT(),您将显式获取记录。即使您没有记录,您也会询问引擎在最坏情况下会返回零的记录数。 Count()和其他聚合一样,预计会有一个分组,所以即使你没有,你仍然会问。
所以引擎基本上都在说嘿......没有记录,但是我必须向你发送一条记录,这样你就可以看到count()列,看看你想做什么。所以,它正在做你所要求的。
现在,对于你问的其他问题的评论......
是的,但我想从项目中计算项目成员,我如何计算所有用户都拥有id_project 1的project_users中的用户。
由于您只关心计数,而不是涉及的具体WHO,您可以直接从project_users表中获取此结果(该表应该在ID_User和ID_Project上的另一个上都有索引。然后
select count(*)
from project_users
where id_project = 1
要从原始问题的基础上扩展以获得额外的细节,我会做...
select
p.id_project,
p.id_plan,
p.name as projectName,
p.description as projectDescription,
pp.name as planName,
pp.description as planDescription,
pp.max_projects,
pp.max_member,
pp.max_filestorage,
PJCnt.ProjectMemberCount
from
( select id_project,
count(*) as ProjectMemberCount
from
project_users
where
id_project = 1 ) PJCnt
JOIN Projects p
on PJCnt.id_project = p.id_project
JOIN Project_Plans PP
on p.id_plan = pp.id_plan
现在,根据表格的这种布局,计划可以有最大成员数,但没有任何内容表示基于所有项目的计划的最大成员,或者每个SINGLE项目的最大成员数。那么,如果一个计划允许20人,那么根据同一计划,10个不同的项目可以有20个人吗?只有你才能知道...的影响只是为了考虑你要求的东西。
答案 1 :(得分:0)
您清理过的查询应如下所示:
同时参见sqlfidle演示:http://sqlfiddle.com/#!2/e693f5/9
SELECT
A.id_plan,
A.name AS plan_name,
A.description AS plan_description,
A.max_projects,
A.max_member,
A.max_filestorage,
B.id_plan,
B.name AS project_name,
B.description AS project_description,
C.id_user,
C.id_project,
COUNT(*) AS max_project_member
FROM
project_plans AS A
LEFT JOIN
projects AS B
ON
B.id_plan = A.id_plan
LEFT JOIN
project_users AS C
ON
C.id_project = B.id_project
WHERE
C.id_project = '".$id."';
这将从select中返回所有null
的{{1}}值,因为您在结果集中有一个合法的返回值,即cols
输出count(*)
。
要解决此问题,只需在末尾添加0
(请参阅示例分组http://sqlfiddle.com/#!2/14d46/2)或
删除group by
,空值将消失,计数(*)值count(*)
在这里查看简单的sql示例:http://sqlfiddle.com/#!2/ab7dd/5
只需注释count(),你修复了你的null问题!