我已经创建了一个小猫数据库,其中我需要使用以下公式。在这方面,我有两个表:出生和处置。
Birth table contains id, dob, owner, date of purchase
disposal table contains id, date of disposal (dodisposal), cause of death, sold, treatment我现在尝试使用下面的MySQL查询对这两个表使用一个公式,但它不起作用。
Select birth.owner, (((select count(disposal.id) from disposal WHERE
dodisposal BETWEEN DATE_SUB(NOW(), INTERVAL 600 DAY) AND NOW()) /
(select count(birth.id) from birth where birth.id not in
(select disposal.id from disposal)
)
) * 100)
from birth left join disposal on
disposal.brandnumber = birth.id group by birth.owner但我一直得到相同的结果,所有的所有者:
即
rita : 79.6
sunita : 79.6
Smith : 79.6我期望的结果应该是通过以下公式得到的:
Number of deaths in the current year / total number of live cats * 100发布于 2018-07-31 14:30:30
我找到了这个问题的解决方案,方法是创建两个独立的视图,然后使用MySQL查询它们的结果。
create view cats as select id, count(disposal.id) from disposal WHERE
dodisposal BETWEEN DATE_SUB(NOW(), INTERVAL 600 DAY) AND NOW()) as dead
create view livecates as select count(birth.id) from birth left join disposal on disposal.id = birth.id where birth.id not in (select disposal.id from disposal) as live
select livecats.id, (dead/live * 100) from livecats left join cats on livecats.id = cats.id group by livecats.idhttps://stackoverflow.com/questions/51590812
复制相似问题