2015-03-19 136 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