【问题标题】:How count result from 3 tables如何计算 3 个表的结果
【发布时间】:2021-11-26 11:26:13
【问题描述】:

请看一下这 3 个表格:

Pets
+----+---------+-------+
| id | petname | owner |
+====+=========+=======+
| 1  | chew    | 1     |
+----+---------+-------+
| 2  | yo      | 2     |
+----+---------+-------+
| 3  | mah     | 3     |
+----+---------+-------+

Owners
+----+-------+-----------+
| id | store | ownername |
+====+=======+===========+
| 1  | 1     | Jonh      |
+----+-------+-----------+
| 2  | 2     | Joe       |
+----+-------+-----------+
| 3  | 3     | Smith     |
+----+-------+-----------+

Stores
+----+------------+
| id | storename  |
+====+============+
| 1  | Lite Store |
+----+------------+
| 2  | Mega       |
+----+------------+
| 3  | Corner     |
+----+------------+

这样有可能得到这个结果吗?

+------------+------------+
| storename  | Total Pets |
+============+============+
| Lite Store | 5          |
+------------+------------+
| Mega       | 8          |
+------------+------------+
| Corner     | 0          |
+------------+------------+

我花了几个小时尝试了很多子查询和连接,但我错过了一些东西,也许是 Union?

在下面我接近了,但仍然很远

SELECT storename, COUNT(distinct stores.storename) as store, count(DISTINCT pets.owner) as petowner from stores inner join owners on owners.id = stores.id inner JOIN pets on pets.owner = stores.id group by stores.id
SELECT stores.storename, COUNT(distinct stores.storename) as store, count(DISTINCT pets.owner) as petowner from stores inner join owners on owners.id = stores.id inner JOIN pets on pets.owner = stores.id group by stores.id
SELECT stores.storename, COUNT(distinct owners.id) as store, count(DISTINCT pets.owner) as petowner from stores inner join owners on owners.id = stores.id inner JOIN pets on pets.owner = stores.id group by stores.id
SELECT COUNT(*),(SELECT COUNT(*) from stores) FROM pets
SELECT COUNT(*),(SELECT DISTINCT(COUNT(*)) from stores),(SELECT DISTINCT(COUNT(*)) FROM owners) FROM pets
SELECT DISTINCT(COUNT(*)), ( select count(DISTINCT(stores.storename)) from stores join owners on stores.id = stores.storename ) FROM pets
select stores.storename, (select count(*) from pets) from stores join owners on stores.id = stores.storename group by storename
select DISTINCT(stores.storename), (select count(*) from pets) from stores join owners on stores.id = stores.storename group by storename
select (count(stores.storename)), (select count(*) from pets) as total from stores join owners on stores.id = stores.storename group by storename

有没有办法得到上面的结果?

任何帮助将不胜感激!

【问题讨论】:

  • 在所需的输出中有“total 5 pets”和“total 8 pets”的行,但在 pets 表中只有 3 只宠物。请解释这个 ot 更新源数据集更合适
  • 请详细说明如何获得预期结果

标签: mysql join subquery union


【解决方案1】:

感谢您提供问题。我已经形成了以下查询。请您尝试一下,看看它是否有效。如果没有,请告诉我

Select s.storename, count(p.petname) from pets p join owners o on p.owner=o.id join stores s on o.store=s.id group by 1

【讨论】:

  • 当然,我从我的 phpamyadmin 历史记录中得到了一些
  • 哇哇哇,它的工作就像魔术一样!谢谢!
猜你喜欢
  • 2018-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-08
  • 2014-10-09
  • 1970-01-01
  • 2020-09-23
  • 1970-01-01
相关资源
最近更新 更多