php分组SQL统计,该怎么处理

WBOY
Release: 2016-06-13 12:14:54
Original
1155 people have browsed it

php分组SQL统计
数据库 aaaaa
id ,name, add
1 , 张三,北京
2,张一,北京
3,张二,落阳
4,李一,成都
5,李三,上海
5,李三,上海 

SELECT name,add FROM aaaaa ORDER BY add

结果:

 张三,北京
张一,北京
李一,成都
张二,落阳
李三,上海
李三,上海 

我想得到的结果:
 张三,北京
张一,北京
北京:2人
李一,成都
成都:1人
张二,落阳
落阳:1人
李三,上海
李三,上海 
上海:2人

请问一下,这个SQL能实现吗?
请详细一些,
谢谢

------解决思路----------------------
SELECT name,add, (select count(*) from aaaaa where add=T.add) as cnt FROM aaaaa T ORDER BY add
将得到
张三,北京,2
张一,北京,2
李一,成都,1
张二,落阳,1
李三,上海,2
李三,上海 ,2
你在输出时判断一下就可以了
------解决思路----------------------
SELECT DISTINCT(name),add, (select count(*) from aaaaa where add=T.add) as cnt FROM aaaaa T ORDER BY add
这样直接就可以了,不用输出判断了

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!