COUNT返回的总行数忽略了MySQL中的LIMIT,为什么?

时间:2018-12-19 04:11:51

标签: mysql count limit sql-limit

今天,当我编写一个复杂的查询时,偶然地发现,即使在查询中设置了LIMIT,MySQL服务器也返回了COUNT的总行数。


示例:

SELECT COUNT(*) FROM `log` LIMIT 10;

输出:

5219

但是,如果我在没有COUNT的情况下运行查询,它将仅返回10行。我的问题是,

  

LIMIT存在时,为什么MySQL会忽略COUNT

2 个答案:

答案 0 :(得分:1)

LIMIT用于返回总结果的子集,在您的情况下,结果仅一行,因此没有效果

答案 1 :(得分:0)

def partition(input :List[Int] // a sorted List of Ints ,prev :Int // Int previously added to the accumulator ,splits :List[List[Int]] // accumulator of Ints for eventual output ): List[List[Int]] = { // the output (same type as accumulator) input match { // what does input look like? case Nil => splits // input is empty, return the accumulator // input has a head and tail, head is close to previous Int case h :: t if h-prev < 2 => // start again with new input (current tail), new previous (current head), // and the current head inserted into accumulator partition(t, h, (h :: splits.head) :: splits.tail) // input has a head and tail, head is not close to previous Int case h :: t => // start again with new input (current tail), new previous (current head), // and the current head is the start of a new sub-list in the accumulator partition(t, h, List(h) :: splits) } } 子句用于选择数量有限的记录的目的,而LIMIT是聚合函数,它将返回聚合结果。一起使用countLIMIT毫无意义,因为COUNT可能会返回任意n条记录。