通过mysql查询使用formulla:error

dsf9zpds  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(322)

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

  1. Birth table contains id, dob, owner, date of purchase
  2. disposal table contains id, date of disposal (dodisposal), cause of death, sold, treatment

我现在尝试使用下面的mysql查询为two表使用一个公式,但它不起作用。

  1. Select birth.owner, (((select count(disposal.id) from disposal WHERE
  2. dodisposal BETWEEN DATE_SUB(NOW(), INTERVAL 600 DAY) AND NOW()) /
  3. (select count(birth.id) from birth where birth.id not in
  4. (select disposal.id from disposal)
  5. )
  6. ) * 100)
  7. from birth left join disposal on
  8. disposal.brandnumber = birth.id group by birth.owner

但我对所有车主的评价都是一样的:
ie公司

  1. rita : 79.6
  2. sunita : 79.6
  3. Smith : 79.6

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

  1. Number of deaths in the current year / total number of live cats * 100
m4pnthwp

m4pnthwp1#

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

  1. create view cats as select id, count(disposal.id) from disposal WHERE
  2. dodisposal BETWEEN DATE_SUB(NOW(), INTERVAL 600 DAY) AND NOW()) as dead
  3. 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
  4. select livecats.id, (dead/live * 100) from livecats left join cats on livecats.id = cats.id group by livecats.id

相关问题