在子查询中使用限制

时间:2017-08-04 19:07:46

标签: php mysql

我想在LIMIT中的子查询中使用MySQL,但每次运行查询时都会出现[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
  );

1 个答案:

答案 0 :(得分: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_id collection_id 进行比较,而非计数。
  • 按集合名称和集合ID进行分组是一回事。