2017-08-04 57 views
1

我試圖在MySQL的子查詢中使用LIMIT,但每次運行查詢時我都會得到[42000][1235] This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME sub query'錯誤。在子查詢中使用限制

我想只得到從子查詢返回的第一個20 ID。

SELECT c.collection_name, t.raw 
FROM collections c 
    JOIN member_of_collection m 
    ON c.collection_id = m.collection_id 
    JOIN citations t 
    ON m.citation_id = t.citation_id 
WHERE m.collection_id IN 
    (
    SELECT count(*) FROM collections c 
     JOIN member_of_collection m 
     ON c.collection_id = m.collection_id 
     JOIN citations t 
     ON t.citation_id = m.citation_id 
    WHERE length(trim(t.raw)) > 0 
      AND RIGHT(c.collection_name, length(c.collection_name) - 4) IN (SELECT cat_name from cats_cat) 
      AND left(t.raw, 3) != '–––' 
    GROUP BY c.collection_name 
    ORDER BY count(*) desc 
    LIMIT 20 
); 
+0

'極限20'需要在子查詢的旁邊。 –

+0

你爲什麼要比較'collection_id'和'count(*)'值? –

回答

1

你可以用join

SELECT c.collection_name, t.raw 
FROM collections c JOIN 
    member_of_collection m 
    ON c.collection_id = m.collection_id JOIn 
    citations t 
    ON m.citation_id = t.citation_id JOIN 
    (SELECT c.collection_id, count(*) 
     FROM collections c JOIN 
      member_of_collection m 
      ON c.collection_id = m.collection_id JOIN 
      citations t 
      ON t.citation_id = m.citation_id 
     WHERE length(trim(t.raw)) > 0 AND 
      RIGHT(c.collection_name, length(c.collection_name) - 4) IN (SELECT cat_name from cats_cat) 
      AND left(t.raw, 3) != '–––' 
     GROUP BY c.collection_id 
     ORDER BY count(*) desc 
     LIMIT 20 
    ) cc 
    ON m.collection_id = cc.collection_id; 

這使得幾個假設:

  • 你真的要比較collection_idcollection_id,不計數。
  • 按集合名稱和集合標識分組是一回事。
+0

這些假設是正確的。我犯了一個錯字。 這解決了它。 謝謝。 – hello