在尝试在查询中执行2次计数时发现此错误
首先向您展示查询:
$sql = mysql_query("select c.id, c.number, d.name,
(select count(*) from `parts` where `id_container`=c.id group by `id_car`) as packcount,
(select count(*) from `parts` where `id_container`=c.id) as partcount
from `containers` as c
left join `destinations` as d on (d.id = c.id_destination)
order by c.number asc") or die(mysql_error());
现在parts
表有两个我需要在计数中使用的字段:
id_car
id_container
id_car
=该部件所用汽车的ID
id_container
=零件所在容器的ID
packcount
我想要的是每个集装箱的总车数
对于partcount
,我想要它计算每个容器的总份数
答案 0 :(得分:2)
这是因为GROUP BY你正在使用
尝试类似
的内容(select count(distinct id_car) from `parts` where `id_container`=c.id)
在你的子查询中(现在无法检查)
修改
PFY - 我认为UNIQUE适用于索引
答案 1 :(得分:0)
您在第一个子查询中的分组导致返回多行,您可能需要运行单独的查询才能获得您要查找的结果。
答案 2 :(得分:0)
此子查询可能会返回多行。
(select count(*) from `parts` where `id_container`=c.id group by `id_car`) as packcount, ...
所以,我建议尝试以下方法:
(select count(DISTINCT `id_car`) from `parts` where `id_container`=c.id) as packcount, ...