4

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

选择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 秒)

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

提前致谢。

4

4 回答 4

2

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

于 2012-10-17T14:00:27.873 回答
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 个表中的数据,我们可以将它们连接到 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
于 2012-10-17T14:31:59.787 回答
1

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

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

结果:

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

于 2012-10-18T13:05:38.677 回答
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
于 2012-10-17T14:05:17.400 回答