简体   繁体   English

具有子句查询分组

[英]group by having clause query

Customer table : (Id, FirstName, LastName, City, Country, Phone) 客户表:(ID,名字,姓氏,城市,国家/地区,电话)

** List the number of customers in each country. **列出每个国家/地区的客户数量。 Only include countries with more than 1 customers. 仅包括拥有超过1个客户的国家/地区。 --> query will be as follows ->查询将如下

 SELECT COUNT(Id), Country
    FROM Customer
    GROUP BY Country
    HAVING COUNT(Id) > 1

Results: 3 records
Count   Country
2   France
4   Germany
3   USA

Problem : I need to get names of these count ie. 问题:我需要获取这些计数的名称,即。 FirstName in the same query EG : as below 同一查询EG中的名字:如下

Results: 3 records
    Count   Names             Country
    2       john,max          France
    4       abc,xyz,aab,cdf   Germany
    3       mmm,fmf,dm        USA

Is it possible? 可能吗?

Try this: 尝试这个:

select count(id) as count,
       group_concat(first_name),
       country
from Customer
group by country

Group Concat function returns a string with concatenated non-NULL value from a group. 组Concat函数从组中返回一个带有串联的非NULL值的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM