2012-02-09 148 views
0

IM歌厅试图做我的查询里面2个字时,这个错误子查询返回多个1个

你第一次生病节目查询:

$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表中有2场是我需要在计数使用: id_car id_container

id_car =汽车的部分是 的ID=容器中的一部分是在

packcount所有我想要的ID是partcount所有每个集装箱 的轿车总量的计数我希望它每个集装箱

+0

您可以通过在你的packcount线有一组和您试图将其作为单列值返回。 – StudyOfCrying 2012-02-09 22:38:26

+0

如果你真的想得到这个帮助,我建议编辑你的问题,包括表定义和数据的例子,以及你的查询所需的输出。谢谢。作为StudyOfCrying, – StudyOfCrying 2012-02-09 22:42:00

+0

说,你可以请把表结构,插入和你想要什么你的查询?它会更快, – 2012-02-09 23:14:12

回答

2

这是因为GROUP BY的您正在使用

试着这么做

(select count(distinct id_car) from `parts` where `id_container`=c.id) 

在你的子查询(不能检查现在)

编辑

PFY - 我认为UNIQUE是索引

+0

截然不同!那就是我正在寻找的!非常感谢 – scarhand 2012-02-09 23:12:16

+0

是的,当我编写这个表时,我正在索引一张表格,并且困惑自己:) – PFY 2012-02-10 03:57:27

0

您的分组的总份数的计数在您的第一个子查询中会导致返回多行,您可能需要运行单独的查询以获得您正在查找的结果。

+0

我真的想在1查询中得到这个工作。 – scarhand 2012-02-09 22:44:14

+0

你有没有尝试过在你的第一个子查询中选择UNIQUE count(id_car)? - 编辑 - Xander击败了我 – PFY 2012-02-09 22:47:54

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, ... 

见:COUNT(DISTINCT) on dev.mysql.com

和:QA on stackoverflow