gpt4 book ai didi

mysql - 基于条件 SELECT 的 COUNT

转载 作者:行者123 更新时间:2023-11-28 23:08:58 24 4
gpt4 key购买 nike

我有一个表,其中包含两个变量,每个变量都出现多次:

Customer        City
Bob New York
Bob Denver
Bob Denver
Greg Denver
Greg Denver
Greg New York

示例:

SELECT Customer FROM tableName WHERE COUNT( SELECT Customer,City FROM tableName)=1;

我想为每个唯一的客户名称获取唯一的城市,并计算该客户拥有多少个城市。你如何在 mysql 中执行此操作?

最佳答案

我不确定你是想为每个客户获取一个城市的重复次数还是不同城市的总数,所以我将同时输入这两个值:

select Customer, City, count(City) as CityCount, TotalDistinctCities
from CustomerCities
join (select Customer as cust, count(distinct City) as TotalDistinctCities
from CustomerCities
group by Customer)
as CityCount on cust=Customer
group by Customer, City, TotalDistinctCities

关于mysql - 基于条件 SELECT 的 COUNT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46453309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com