简体   繁体   中英

select distinct and count distinct in mysql

Is this possible Selecting distinct rows from a table column and count repeated row for each distinct fields in a single query

$sql = "SELECT DISTINCT location and COUNT(DISTINCT location) 
        FROM ".$db_name.".$table_name 
        ORDER BY location ASC 
        LIMIT ". $start_page.",". $recordPer_page."";

Hi this query help you:

SQLFIDDLE example

SELECT   `location`, COUNT(`location`) 
    FROM     tbl   
    GROUP BY `location`
    ORDER BY `location`

Data:

    ('Africa'),
    ('Viena'),
    ('Mexico'),
    ('Chicago'),
    ('Miami'),
    ('Chicago'),
    ('Viena'),
    ('London'),
    ('Viena');

Result:

| LOCATION | COUNT(`LOCATION`) |
--------------------------------
|   Africa |                 1 |
|  Chicago |                 2 |
|   London |                 1 |
|   Mexico |                 1 |
|    Miami |                 1 |
|    Viena |                 3 |

Try,

SELECT   `location`, COUNT(DISTINCT `location`) 
FROM     ....   
GROUP BY `Location`
ORDER BY ....
LIMIT    ....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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