php - 在子查询中使用限制

标签 php mysql

我正在尝试在 MySQL 的子查询中使用 LIMIT,但每次运行查询时,我都会得到 [42000][1235] This version MySQL 还不支持 '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
  );

最佳答案

你可以只使用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 进行比较,而不是计数。
  • 按集合名称和集合 id 分组是一回事。

关于php - 在子查询中使用限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45513900/

相关文章:

MySQL根据相同的值计算

mysql - PHP/Mysql 日历

mysql查询的PHP while循环有时会随机返回一个空集(它们不为空,但结果为空)

PHP 防止 F5 重新提交 |不带重定向

php - 架构和框架选择团队问题

php - DomDoc/SimpleXML/XSLT : parsing to add auto-incrementing id attributes to each unique element child of an element

php - 从多个复选框发布php mysql

php - MySQL VALUES() 函数不适用于 Codeigniter db->query()

php - 在存储库模式中保存关系的好方法是什么?

php - 使用 PHP 进行 MySQL 查询