0

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."";
1
  • SELECT DISTINCT(location), COUNT(DISTINCT(location)) FROM .... Commented Oct 25, 2012 at 15:30

2 Answers 2

3

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 |
Sign up to request clarification or add additional context in comments.

Comments

0

Try,

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

1 Comment

returns 1 in second column for all rows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.