Mysql 子查询返回超过1行

Mysql 子查询返回超过1行,mysql,sql,count,Mysql,Sql,Count,当我试图在我的查询中进行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`

当我试图在我的查询中进行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\u车
id\u容器

id\u car
=零件所在汽车的id
id\u容器
=零件所在容器的id

对于
packcount
来说,我想要的是每个集装箱的总车数
对于
partcount
all i want it每个容器的总零件数您在第一个子查询中的分组导致返回多行,您可能需要运行单独的查询以获得您要查找的结果。

这是因为您使用的是GROUP BY

试试像这样的东西

(select count(distinct id_car) from `parts` where `id_container`=c.id)
在您的子查询中(现在无法检查)

编辑


PFY-我认为索引是唯一的

此子查询可能返回多行

(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, ...
见:


并且:

您的packcount行中有一个group by,您试图将其作为一个单列值返回。如果您确实需要这方面的帮助,我建议编辑您的问题,以包括表定义和数据的示例以及查询所需的输出。谢谢。正如StudyOfCrying所说的,你能把表的结构、插入和你想从查询中得到什么吗?这样会快得多。您在第一个子查询中是否尝试过选择唯一计数(id_car)-编辑-赞德把我打败了,我在写一张桌子的中间,当我写了这篇文章,把自己弄糊涂了: