我创建了一个小猫数据库,我需要在其中使用以下公式。对此我有两个表:出生和处置。
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
最佳答案
我找到了这个问题的解决方案,方法是创建两个单独的 View ,然后使用 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.id
关于php - 通过mysql查询使用公式: error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51590812/