本文介绍了Mysql 查询 [0001]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 2 个 MySQL,我正在尝试获取每个城市至少拥有一只狗的主人数量.我的表是:
I have 2 MySQL and I'm trying to get the number of owners who own at least one dog, for each city. My tables are:
owners
id, name, city
和
dogs
id, owner_id, name, weight
到目前为止,我有以下几点:
I have the following so far:
SELECT owners.city, count(dogs.id) AS 'Owners'
FROM owners INNER JOIN dogs ON (owners.id = dogs.owner_id)
GROUP BY owners.city
然而,这给了我每个城市的狗总数.但我想知道每个城市的养狗人数.
However, this gives me the total number of dogs per city. But I'd like to get the number of dog owners per city.
我该怎么做?
推荐答案
你必须只计算带有 DISTINCT 字的所有者 id,它只计算唯一的所有者.
You have to count only owners id with DISTINCT word, which counts only unique owners.
SELECT owners.city, count(DISTINCT owners.id) AS 'Owners'
FROM owners INNER JOIN dogs ON (owners.id = dogs.owner_id)
GROUP BY owners.city
这篇关于Mysql 查询 [0001]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!