0
我正在嘗試計算給定查詢的行數。但count會返回比它應該更多的行。發生什麼事?MySQL計數返回比它應該更多的行
該查詢只返回1行。
select *
from `opportunities`
inner join `companies` on `opportunities`.`company_id` = `companies`.`id`
left join `opportunityTags` on `opportunities`.`id` = `opportunityTags`.`opportunity_id`
where `opportunities`.`isPublished` = '1' and `opportunities`.`Company_id` = '1'
group by `opportunities`.`id` ;
該查詢返回有3行。
select count(*) as aggregate
from `opportunities`
inner join `companies` on `opportunities`.`company_id` = `companies`.`id`
left join `opportunityTags` on `opportunities`.`id` = `opportunityTags`.`opportunity_id`
where `opportunities`.`isPublished` = '1' and `opportunities`.`Company_id` = '1'
group by `opportunities`.`id`;
您在此表中有多少條記錄?並且,如果您將刪除JOIN,請檢查查詢是否正確計算。 – 2014-11-14 13:49:46
儘管(刺激)更具有表演性,但在沒有任何聚合函數的情況下,使用GROUP BY子句(在您的第一個查詢中)是不恰當的並且經常會引起誤解。也許你正在考慮DISTINCT - 雖然在對'SELECT *'使用時沒有任何意義(在正確數據庫中) – Strawberry 2014-11-14 14:46:44