如何计算 3 个表的结果
How count result from 3 tables
请看一下这 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
有什么方法可以得到上面的结果吗?
任何帮助将不胜感激!
感谢您提供查询。我形成了以下查询。请求您尝试一下,看看是否有效。如果没有,请告诉我
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
请看一下这 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
有什么方法可以得到上面的结果吗?
任何帮助将不胜感激!
感谢您提供查询。我形成了以下查询。请求您尝试一下,看看是否有效。如果没有,请告诉我
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