2015-03-19 88 views
0

任何人都可以解释如何解决这个问题吗?仅返回MySQL中列的最高值

SELECT book_id, title, count(DISTINCT order_id) 
FROM a_bkinfo.books 
JOIN a_bkinfo.book_topics USING (book_id) 
JOIN a_bkorders.order_details USING (book_id) 
WHERE topic_id IN ("FCT", "POE") 
GROUP BY book_id; 


1077 Programming for Poets 2 
1103 Selected Poems 34 
1133 Leaves of Grass 4 
1304 Stories of Discoveries 6 
1306 Myths of SQL 3 
1602 Goblin Market and Other Poems 4 

我只想要返回右侧最高值的行(即“Selected Poems”)。我将如何去完成这件事?

+1

什么是努力实现? – 2015-03-19 01:45:10

+0

我试图让'max(count(DISTINCT order_id))'返回一个数字而不是一个错误。 – Blobert 2015-03-19 02:09:36

+0

完成此操作的典型方法是按降序对计数表达式进行排序,然后将结果限制为一行。如果你需要关系,那么你可能不得不采取其他方法之一。 – shawnt00 2015-03-19 03:10:07

回答

0
select max(order_count) from (
    SELECT count(DISTINCT order_id) as order_count 
    FROM a_bkinfo.books 
    JOIN a_bkinfo.book_topics USING (book_id) 
    JOIN a_bkorders.order_details USING (book_id) 
    WHERE topic_id IN ("FCT", "POE") 
    GROUP BY book_id 
) T 

这是一个标量的结果,所以你应该能够添加到您的原始查询的选择列表,如果你想看到它附加到每一行:

select ..., (
    select max(order_count) from (
     SELECT count(DISTINCT order_id) as order_count 
     FROM a_bkinfo.books 
     JOIN a_bkinfo.book_topics USING (book_id) 
     JOIN a_bkorders.order_details USING (book_id) 
     WHERE topic_id IN ("FCT", "POE") 
     GROUP BY book_id 
    ) as max_order_count 
from ... 

,或者您可以使用交叉加入

select ... 
from ... 
    JOIN a_bkorders.order_details USING (book_id), 
    (
     select max(order_count) from (
      SELECT count(DISTINCT order_id) as order_count 
      FROM a_bkinfo.books 
      JOIN a_bkinfo.book_topics USING (book_id) 
      JOIN a_bkorders.order_details USING (book_id) 
      WHERE topic_id IN ("FCT", "POE") 
      GROUP BY book_id 
     ) T 
    ) T2 
where ... 

不确定哪个是MySQL的更好选择。

这最后一个选项与最大值的行(S)将返回:

SELECT book_id, title, count(DISTINCT order_id) 
FROM a_bkinfo.books 
JOIN a_bkinfo.book_topics USING (book_id) 
JOIN a_bkorders.order_details USING (book_id) 
WHERE topic_id IN ("FCT", "POE") 
GROUP BY book_id 
HAVING count(DISTINCT order_id) = (
    select max(order_count) from (
     SELECT count(DISTINCT order_id) as order_count 
     FROM a_bkinfo.books 
     JOIN a_bkinfo.book_topics USING (book_id) 
     JOIN a_bkorders.order_details USING (book_id) 
     WHERE topic_id IN ("FCT", "POE") 
     GROUP BY book_id 
    ) 
) 
+0

谢谢。我会在什么地方把它放在原来的区块中,这样它会返回右边那个数字最高的那一行? HAVING count(DISTINCT order_id)= select max(order_count)from( SELECT count(DISTINCT order_id)as order_count FROM a_bkinfo.books JOIN a_bkinfo.book_topics USING(book_id) JOIN a_bkorders.order_details USING(book_id) WHERE topic_id IN(“FCT”,“POE”) GROUP BY book_id )T'但它返回一个错误。 – Blobert 2015-03-19 02:58:24

+0

也许我仍然错误地想要什么。如果你想使用'having',那么把整个查询包装在另一组圆括号中,并从最后的T中包装。我不确定它会在MySQL上工作,但它应该可以在其他平台上工作。 – shawnt00 2015-03-19 03:08:15

+0

我只想返回具有最高命令(并且只有那本书)的书 - 具有最高输出的书:'(count(DISTINCT order_id)'。是否有用MySQL来做这件事?'HAVING count( DISTINCT order_id)= max(count(DISTINCT order_id)'由于某种原因返回一个错误,但是'HAVING count(DISTINCT order_id)= 34'似乎有效,所以我不确定错在哪里。 – Blobert 2015-03-19 03:13:19