【问题标题】:MySQL ordering by distinct countMySQL按不同计数排序
【发布时间】:2012-10-17 13:55:01
【问题描述】:

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

选择customers.city,books.title 来自借来的、书籍、客户 借出的.userID = 客户.userID 和出借的.bookID = books.bookID +------------+-------------------+ |城市|标题 | +------------+-------------------+ |哈罗盖特 |十字兔 | |哈罗盖特 | PHP 和 MySQL Web 开发 | |哈罗盖特 | PHP 和 MySQL Web 开发 | |怀特黑文 |希腊神话 | |怀特黑文 |恐龙翱翔 | |怀特黑文 |恐龙翱翔 | |出售 |魔术技巧 | |出售 |魔术技巧 | |出售 |魔术技巧 | |出售 |恐龙翱翔 | |出售 |恐龙翱翔 | +------------+-------------------+ 11 行(0.00 秒)

我想找到每个城市最受欢迎的标题,所以我做了以下操作:

按城市分组 按计数排序(不同的标题) desc

但是这不会产生正确的结果。我明白了:

+------------+-------------------+ |城市|标题 | +------------+-------------------+ |出售 |恐龙翱翔 | |怀特黑文 |恐龙翱翔 | |哈罗盖特 | PHP 和 MySQL Web 开发 | +------------+-------------------+ 3 行一组(0.00 秒)

这似乎是按字母顺序排序的,而不是按受欢迎程度排序的。 获得数据后,我认为按照我的要求对其进行排序很容易,但事实并非如此。 我是否需要进行某种联接或更复杂的事情?

提前致谢。

【问题讨论】:

    标签: mysql sorting


    【解决方案1】:

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

    【讨论】:

    • 不,那也不行。
    【解决方案2】:

    我会分 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 个表的数据,我们可以将它们连接到 city 和 count,以获得在两个表上匹配的相应书籍。

    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
    

    【讨论】:

      【解决方案3】:

      感谢所有回答的人。由于这是一个测试问题,我不想“剪切和粘贴”其他人的工作,而是使用他们的逻辑进行我自己的查询。这是我得到的:

      选择城市,标题 从 ( 选择customers.city 作为城市,books.title 作为标题,count(books.title) 作为cnt 来自书籍、客户、借来的 借出的.userID = 客户.userID 和出借的.bookID = books.bookID 按标题、城市分组 按cnt desc订购)作为tbl 按城市分组

      结果:

      +------------+-------------------+ |城市|标题 | +------------+-------------------+ |哈罗盖特 | PHP 和 MySQL Web 开发 | |出售 |魔术技巧 | |怀特黑文 |恐龙翱翔 | +------------+-------------------+ 3 行一组(0.00 秒)

      【讨论】:

        【解决方案4】:

        我正在删除客户表,因为该表没有输出

        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
        

        【讨论】:

        • 是的,这是我从那里获得客户城市的地方。这不存储在外借或书籍表中。
        • 感谢感谢作品,但它现在有第三列(标题数量),并且还显示了所有其他标题的数量。有什么方法可以显示每个城市最受欢迎的书吗?
        猜你喜欢
        • 2016-01-08
        • 2012-02-07
        • 1970-01-01
        • 1970-01-01
        • 2019-02-20
        • 1970-01-01
        • 2012-03-09
        • 2016-05-12
        • 1970-01-01
        相关资源
        最近更新 更多