2012-10-25 78 views
0

不同的是這可能從一個表列中選擇不同的行和在單個查詢選擇不同和計數在MySQL

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

SELECT DISTINCT(位置),COUNT(DISTINCT(位置))FROM .... – scalopus

回答

1

嘗試計數重複的行爲每個不同的領域,

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

你好,這查詢幫助你:

SQLFIDDLE example

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

數據:

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

結果:

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