我有產生以下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)
這似乎是按字母順序排序,而不是受歡迎程度。 獲得了數據後,我認爲可以很容易地按照我的要求排序,但結果並不是這樣。 我需要做某種連接或比這更復雜的事情嗎?
在此先感謝。
不,不這樣做,要麼。 –