Postgresql查询为更广泛的搜索提供的结果较少

时间:2016-08-24 05:15:16

标签: sql postgresql

我面临一个非常奇怪的问题,即查询。

我的查询非常抱怨,因此我会使用更简单的版本。

select distinct on (a.batch) c.id,a.partid,b.partname,c.stock_stock,
from components a 
join parts b using(partid)
left join (select * from componentsreport($1)) c using (partid)
JOIN start_work d on d.batchid=a.batchid
where  b.issub
group by c.id,a.partid,b.partname,c.stock
order by a.batch;

此查询为c.id=3436提供了许多行,但只有1行:

3436 124  'CPU-A' 450

但是当我添加另一个标准c.id=3436时:

select distinct on (a.batch) c.id,a.partid,b.partname,c.stock_stock,
from components a 
join parts b using(partid)
left join (select * from componentsreport($1)) c using (partid)
JOIN start_work d on d.batchid=a.batchid
where  c.id=3436 and b.issub
group by c.id,a.partid,b.partname,c.stock
order by a.batch;

我得到6行:

3436 124  'CPU-A' 450
3436 125  'CPU-A' 130
3436 125  'CPU-A' 660
3436 126  'CPU-A' 0
3436 127  'CPU-A' 40
3436 128  'CPU-A' 40

哪个是对的!

我不明白当我添加更多条件时,我会获得更多行?看起来有些东西被搞砸了!

我正在使用PostgreSQL 9.3.3

请告知可能导致此问题的原因。

1 个答案:

答案 0 :(得分:0)

这是正确的结果。原因是,你没有Array ( [0] => Array ( [Is_Hide] => 0 [Key] => LHo0VTLsFcI7wWq2EWQy1nUp5U13pXWLXt8s0775 ) [1] => Array ( [Is_Hide] => 0 [Key] => GiSKlOZXRhXTmHKLAeEMPUWoJMs08ftWeenCMrPk ) [2] => Array ( [Is_Hide] => 0 [Key] => LHo0VTLsFcI7wWq2EWQy1nUp5U13pXWLXt8s0775 ) ) ,只是截然不同。

F.ex。你有以下数据:

group by a.batch

在分组后选择不同,订单将为您提供1个唯一批次= c.id = 3436, a.batch = 1 c.id = 3436, a.batch = 2 c.id = 3437, a.batch = 1 c.id = 3437, a.batch = 2

选择哪里会给你2个唯一的a.batch:11

因此,要修复此查询,您需要将select的第一个变量包装为另一个带条件的变体。

2