php - 通过mysql查询使用公式: error

标签 php mysql

我创建了一个小猫数据库,我需要在其中使用以下公式。对此我有两个表:出生和处置。

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/

相关文章:

php - 拉维尔/PHP : Order By Alphabetical with numbers in order

php - 使用 Socialite 在 laravel 中的 null 上调用成员函数 create()

php - 更改php中分页的目标页面

php - 左连接 | ORDER BY 查询速度

mysql find_in_set 数组分隔符

mysql - 计算给定列的条目数

PHP $_SESSION 数据返回空

php - Laravel RouteServiceProvider 中的显式路由模型绑定(bind)问题

php - 在唯一键上使用空值执行 "INSERT...ON DUPLICATE KEY UPDATE"的变通解决方案

php - 通知: Undefined index of a xml