2012-10-17 56 views
4

我有產生以下SELECT查詢:MySQL的排序由重複計數

 
select customers.city , books.title                 
from loaned, books, customers                   
where loaned.userID = customers.userID                 
and loaned.bookID = books.bookID 
+------------+-------------------------------+ 
| city  | title       | 
+------------+-------------------------------+ 
| Harrogate | The cross rabbit    | 
| Harrogate | PHP and MySQL web development | 
| Harrogate | PHP and MySQL web development | 
| Whitehaven | Greek Mythology    | 
| Whitehaven | Dino-soaring     | 
| Whitehaven | Dino-soaring     | 
| Sale  | Magic tricks     | 
| Sale  | Magic tricks     | 
| Sale  | Magic tricks     | 
| Sale  | Dino-soaring     | 
| Sale  | Dino-soaring     | 
+------------+-------------------------------+ 
11 rows in set (0.00 sec) 

我要找到每個城市最流行的稱號,所以我做了以下內容:

 
group by city 
order by count(distinct title) desc 

但是這不會產生正確的結果。我得到:

 
+------------+-------------------------------+ 
| city  | title       | 
+------------+-------------------------------+ 
| Sale  | Dino-soaring     | 
| Whitehaven | Dino-soaring     | 
| Harrogate | PHP and MySQL web development | 
+------------+-------------------------------+ 
3 rows in set (0.00 sec) 

這似乎是按字母順序排序,而不是受歡迎程度。 獲得了數據後,我認爲可以很容易地按照我的要求排序,但結果並不是這樣。 我需要做某種連接或比這更復雜的事情嗎?

在此先感謝。

回答

1

感謝大家的回答。由於這是一個測試問題,我不想'剪切和粘貼'其他人的工作,而是使用自己的邏輯做出自己的查詢。下面是我的了:

 
select city, title 
from (
    select customers.city as city, books.title as title, count(books.title) as cnt 
    from books, customers, loaned 
    where loaned.userID = customers.userID 
    and loaned.bookID = books.bookID 
    group by title, city 
    order by cnt desc) as tbl 
group by city 

結果:

 
+------------+-------------------------------+ 
| city  | title       | 
+------------+-------------------------------+ 
| Harrogate | PHP and MySQL web development | 
| Sale  | Magic tricks     | 
| Whitehaven | Dino-soaring     | 
+------------+-------------------------------+ 
3 rows in set (0.00 sec) 

2

嘗試更換distinct title只有title,這應該解決您的問題..

+1

不,不這樣做,要麼。 –

0

我刪除客戶表,因爲沒有從該表輸出

select customers.city , books.title , count(books.title) Total      
from loaned, books, customers                   
where loaned.userID = customers.userID              
and loaned.bookID = books.bookID 
    group by customers.city , books.title order by 3 desc 
+0

是的,這是我從哪裏得到客戶的城市。這不是存儲在借出或書籍表中。 –

+0

謝謝感謝作品,但它現在有第三欄(標題數量),還顯示了其他所有標題的數量。有沒有什麼方法可以展示每個城市最受歡迎的書籍? –

1

我想解決這個問題分3步。首先得到每個城市的每本書的數量。

select customers.city, books.title, count(books.title) as count 
from loaned, books, customers 
where loaned.userID = customers.userID 
and loaned.bookID = books.bookID 
group by customers.city, books.title 

該查詢將返回以下行。

+------------+-------------------------------+-------+ 
| city  | title       | count | 
+------------+-------------------------------+-------+ 
| Harrogate | The cross rabbit    | 1  | 
| Harrogate | PHP and MySQL web development | 2  | 
| Whitehaven | Greek Mythology    | 1  | 
| Whitehaven | Dino-soaring     | 2  | 
| Sale  | Magic tricks     | 3  | 
| Sale  | Dino-soaring     | 2  | 
+------------+-------------------------------+-------+ 

使用該數據,然後我將使用該數據在每個城市進行分組數量最多的城市。

select city, max(count) as count 
from 
(
    select customers.city , books.title, count(books.title) as count 
    from loaned, books, customers 
    where loaned.userID = customers.userID 
    and loaned.bookID = books.bookID 
    group by customers.city, books.title 
) as city_book_max_count 
group by city 

將返回這些行,

+------------+-------+ 
| city  | count | 
+------------+-------+ 
| Harrogate | 2  | 
| Whitehaven | 2  | 
| Sale  | 3  | 
+------------+-------+ 

使用從2個表中的數據,我們就可以加入他們的城市和計數,來取得與這兩個表的相應書籍。

select city_book_count.city, city_book_count.title 
from 
(
    select customers.city , books.title, count(books.title) as count 
    from loaned, books, customers 
    where loaned.userID = customers.userID 
    and loaned.bookID = books.bookID 
    group by customers.city, books.title 
) as city_book_count 
join 
(
    select city, max(count) as count 
    from 
    (
    select customers.city , books.title, count(books.title) as count 
    from loaned, books, customers 
    where loaned.userID = customers.userID 
    and loaned.bookID = books.bookID 
    group by customers.city, books.title 
) as city_book_count_temp 
    group by city 
) as city_book_max_count 
on city_book_count.city = city_book_max_count.city 
    and city_book_count.count = city_book_max_count.count