如何选择表组中的所有行和sql中每个组的联系计数 [英] how do select all rows from table groups and count of contacts for each groups in sql

查看:77
本文介绍了如何选择表组中的所有行和sql中每个组的联系计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



i有2个表:1-组2-联系人



组表fileds是:

1-group_id

2-group_name



联系人表格文件是:

1-contact_group_id

2-contact_id

3-contact_name





现在,我想要选择所有群组的联系人数



例如:



groupname contacts数量



frined 12

学校8

敌人0

家庭25 < br $> b $ b

i希望所有群体显示(包括没有任何联系人的群组)



非常感谢

解决方案

  SELECT  group_name  AS  groupname,COUNT(contact_group_id) AS  [联系人计数]  FROM  groups  LEFT   JOIN  contacts  ON  
group_id = contact_group_id GROUP BY group_name,contact_group_id


hi all

i have 2 table : 1- groups 2- contacts

groups table fileds are :
1-group_id
2-group_name

contacts table fileds are :
1-contact_group_id
2-contact_id
3-contact_name


now , i want select all groups with count of group's contacts

example :

groupname contacts count

frined 12
school 8
Enemy 0
family 25

i want all groups dispaly(include groups that have not any contacts)

thanks a lot

解决方案

SELECT group_name AS groupname, COUNT(contact_group_id) AS [contacts count] FROM groups LEFT JOIN contacts ON
group_id = contact_group_id GROUP BY group_name, contact_group_id


这篇关于如何选择表组中的所有行和sql中每个组的联系计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆