MySQL按顺序排序

时间:2012-10-17 13:55:01

标签: mysql sorting

我有一个select查询,它产生以下内容:

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)

这似乎是按字母顺序排序,而不是按人气排序。 获得数据后,我认为按照我的要求订购数据很容易,但事实并非如此。 我是否需要进行某种加入或比这更复杂的事情?

提前致谢。

4 个答案:

答案 0 :(得分:2)

尝试仅使用distinct title替换title,这可以解决您的问题..

答案 1 :(得分:1)

我将分三步解决这个问题。首先得到每个城市每本书的数量。

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

答案 2 :(得分: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)

答案 3 :(得分:0)

我正在删除customer表,因为此表没有输出

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