我正在尝试计算给定查询的行数。但是count返回的行数比它应该返回的行多。这是怎么回事?
此查询仅返回一行。
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`;发布于 2014-11-14 21:56:55
当您选择计数(*)时,它在group by之前计数。您可能可以(不幸的是,我的领域是SQL Server,并且我没有要测试的mySQL实例)通过使用over()函数来解决这个问题。
例如:
select count(*) over (partition by `opportunities`.`id`)编辑:实际上在mySQL中看起来不像是可用的,我的错。把整个事情包装在一个新的select语句中怎么样?这不是最优雅的解决方案,但会给你你想要的图形。
https://stackoverflow.com/questions/26931394
复制相似问题