首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过mysql查询使用公式:错误

通过mysql查询使用公式:错误
EN

Stack Overflow用户
提问于 2018-07-30 17:25:30
回答 1查看 47关注 0票数 0

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

代码语言:javascript
复制
Birth table contains id, dob, owner, date of purchase

disposal table contains id, date of disposal (dodisposal), cause of death, sold, treatment

我现在尝试使用下面的MySQL查询对这两个表使用一个公式,但它不起作用。

代码语言:javascript
复制
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

但我一直得到相同的结果,所有的所有者:

代码语言:javascript
复制
rita   : 79.6
sunita : 79.6
Smith  : 79.6

我期望的结果应该是通过以下公式得到的:

代码语言:javascript
复制
Number of deaths in the current year / total number of live cats * 100
EN

回答 1

Stack Overflow用户

发布于 2018-07-31 14:30:30

我找到了这个问题的解决方案,方法是创建两个独立的视图,然后使用MySQL查询它们的结果。

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51590812

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档