2013-06-05 57 views
1

我有这两个我正在运行的查询。第一个工作,但由于某种原因,EXISTS()函数似乎增加了大约一分钟的加载时间,这使得它难以忍受。所以我写了第二个查询,我觉得应该给出相同的答案,但它给出了一个非常不同的答案。给出不同结果的两个MySQL查询

首先

select 
count(`FavoritesHeaderID`) `count` 
from `favoritesheader` 
join `vwactiveevent` on `vwactiveevent`.`MainEventID`=`favoritesheader`.`MainEventID` 
where `Admin`=0 
and exists(
select 1 
from `invoiceheader` 
join `vwactiveevent` on `vwactiveevent`.`MainEventID`=`invoiceheader`.`MainEventID` 
where `Phone`=`favoritesheader`.`CellPhone` 
and `OrderStatusID`=2 
); => 284 

select 
count(`FavoritesHeaderID`) `count` 
from `favoritesheader` 
join `vwactiveevent` on `vwactiveevent`.`MainEventID`=`favoritesheader`.`MainEventID` 
join `invoiceheader` on `vwactiveevent`.`MainEventID`=`invoiceheader`.`MainEventID` 
where `Admin`=0 
and `Phone`=`favoritesheader`.`CellPhone` 
and `OrderStatusID`=2; => 1578 

我不知道,如果只是这是足够的信息来熄灭的,但如果它是那么任何帮助将非常赞赏。

+0

关于什么的结果不同? – Joe

+0

我的不好,现在更新 –

回答

1

机会是JOINCOUNT中包含重复的行。如果我正确理解你的问题,假设FavoritesHeaderID是独一无二的,这就是你要COUNT领域,你可以添加DISTINCT每个查询,他们应该返回相同的计数:

select 
    count(distinct `FavoritesHeaderID`) `count` 
from `favoritesheader` 
    join `vwactiveevent` on `vwactiveevent`.`MainEventID`=`favoritesheader`.`MainEventID` 
    join `invoiceheader` on `vwactiveevent`.`MainEventID`=`invoiceheader`.`MainEventID` 
where `Admin`=0 
    and `Phone`=`favoritesheader`.`CellPhone` 
    and `OrderStatusID`=2 
+0

你一定知道你的东西,特别是当我第一次发布这个,我错过了在问题中的信息。谢谢 –

+0

@stumpx - np,很高兴我能帮忙! – sgeddes

相关问题