PHP Group SQL Statistics, how to handle

Source: Internet
Author: User
PHP Group SQL Statistics
Database AAAAA
ID, name, add
1, Zhang San, Beijing
2, Zhang Yi, Beijing
3, Zhang Yi, Luo Yang
4, Lee, Chengdu
5, lie triple, Shanghai
5, lie triple, Shanghai

SELECT Name,add from AAAAA ORDER by add

Results:

Zhang San, Beijing
Zhang Yi, Beijing
Lee, Chengdu
Zhang Yi, Luo Yang
Lie triple, Shanghai
Lie triple, Shanghai

I want to get the result:
Zhang San, Beijing
Zhang Yi, Beijing
Beijing: 2 People
Lee, Chengdu
Chengdu: 1 People
Zhang Yi, Luo Yang
Drop Yang: 1 people
Lie triple, Shanghai
Lie triple, Shanghai
Shanghai: 2 people

Excuse me, can this SQL be implemented?
Please give some details,
Thank you

------to solve the idea----------------------
Select Name,add, (select COUNT (*) from AAAAA where Add=t.add) as CNT from AAAAA T ORDER by add
Will get
Zhang San, Beijing, 2
Zhang Yi, Beijing, 2
Lee, Chengdu, 1
Zhang Yi, Luo Yang, 1
Lie triple, Shanghai, 2
Lie triple, Shanghai, 2
You can judge it on the output.
------to solve the idea----------------------
Select DISTINCT (name), add, (SELECT COUNT (*) from AAAAA where Add=t.add) as CNT from AAAAA T ORDER by add
This is straightforward, no output to judge.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.