简体   繁体   English

使用 LIMIT 计算 MySQL 记录

[英]Counting MySQL records with a LIMIT

As I am trying to count the number of records in a table, even when the SQL statement has a LIMIT into it, overall it works, however something weird happens, the code:当我试图计算表中的记录数时,即使 SQL 语句中有一个LIMIT ,总体上它也能工作,但是发生了一些奇怪的事情,代码:

$sql = "SELECT COUNT(*) AS count FROM posts
        ORDER BY post_date DESC
        LIMIT 5";

// ... mysql_query, etc

while($row = mysql_fetch_array($result))
{
    // ... HTML elements, etc
    echo $row['post_title'];

    // ... HTML elements, etc

    echo $row['count']; // this displays the number of posts (which shows "12").
}

Although, when displaying through the while loop, it displays this:虽然,当通过while循环显示while ,它显示:

Notice: Undefined index: post_title in /Applications/MAMP/htdocs/blog/index.php on line 55注意:未定义索引:第 55 行 /Applications/MAMP/htdocs/blog/index.php 中的 post_title

If I remove the COUNT(*) AS count , everything will display perfectly... how come it's doing this?如果我删除COUNT(*) AS count ,一切都会完美显示......它怎么会这样做?

Don't use COUNT(*) to count the number of rows (for a lot of reasons).不要使用COUNT(*)来计算行数(出于很多原因)。 Write out your full query, and add SQL_CALC_FOUND_ROWS right after SELECT :写出您的完整查询,并在SELECT之后添加SQL_CALC_FOUND_ROWS

SELECT SQL_CALC_FOUND_ROWS id, title FROM foo LIMIT 5;

Then, after that query executed (right after), run:然后,在执行该查询之后(紧接着),运行:

SELECT FOUND_ROWS();

That will return the number of rows the original SELECT would have returned if you didn't have the LIMIT on the end (accounting for all joins and where clauses).如果最后没有LIMIT (考虑所有连接和 where 子句),这将返回原始SELECT将返回的行数。

It's not portable, but it's very efficient (and IMHO the right way of handling this type of problem).它不是便携式的,但它非常有效(恕我直言,这是处理此类问题的正确方法)。

LIMIT does not do anything here because you're selecting a single scalar. LIMIT在这里不做任何事情,因为您选择的是单个标量。 The error is shown because you are not selecting the post title, so it is not in the $row hash.显示错误是因为您没有选择帖子标题,因此它不在$row哈希中。

This is happening because COUNT() is an aggregate function.发生这种情况是因为COUNT()是一个聚合函数。 You will have to do two separate queries in order to get both the count of rows in the table and separate records.您将不得不执行两个单独的查询,以获取表中的行数和单独的记录。

I had the same problem and found this article: http://www.mysqldiary.com/limited-select-count/ best way (especially for big tables) is using it like this:我遇到了同样的问题,发现这篇文章: http : //www.mysqldiary.com/limited-select-count/最好的方法(特别是对于大表)是这样使用它的:

SELECT COUNT(*) AS count FROM (SELECT 1 FROM posts
    ORDER BY post_date DESC
    LIMIT 5) t

now it returns a number between 0 and 5现在它返回一个 0 到 5 之间的数字

You're asking the SELECT statement to return only COUNT(*) AS count .您要求SELECT语句仅返回COUNT(*) AS count If you want more columns returned, you have to specify them in the SELECT statement.如果您希望返回更多列,则必须在SELECT语句中指定它们。

By including COUNT(*) AS count you've removed the actual columns that you want to get the information from.通过包含COUNT(*) AS count您已经删除了要从中获取信息的实际列。 Try using尝试使用

SELECT *, COUNT(*) AS count FROM posts ORDER BY post_date DESC LIMIT 5

That query will accomplish both things you're trying to do in one query, but really, these should be broken out into 2 separate queries .该查询将完成您在一个查询中尝试做的两件事,但实际上,这些应该分解为 2 个单独的查询 What is the overall purpose for having a COUNT in the query?在查询中使用 COUNT 的总体目的是什么? It should be done a different way.它应该以不同的方式完成。

You're mixing up an aggregate function COUNT() with a standard selection.您将聚合函数COUNT()与标准选择混合在一起。 You can't get an accurate post title without a GROUP BY clause in your aggregate query.如果您的聚合查询中没有 GROUP BY 子句,您将无法获得准确的帖子标题。 The easiest thing you should do is to do this two queries - one for your count, the other for your post information.您应该做的最简单的事情是执行这两个查询 - 一个用于您的计数,另一个用于您的帖子信息。 Also, there's no sense in using LIMIT on an aggregate function with no other columns in your (omitted) GROUP BY - the current query will always return one row.此外,在(省略) GROUP BY没有其他列的聚合函数上使用LIMIT是没有意义的 - 当前查询将始终返回一行。

What everyone is trying to say is that you should opt to use 2 separate queries instead of 1 single one.每个人都想说的是,您应该选择使用 2 个单独的查询,而不是 1 个单独的查询。

1) get row count of table 1) 获取表的行数

$sql = "SELECT COUNT(*) AS count FROM posts;

2) get last 5 rows of table 2) 获取表格的最后 5 行

$sql = "SELECT * FROM posts
        ORDER BY post_date DESC
        LIMIT 5";

This works for me:这对我有用:

$sql = "
     SELECT COUNT( po.id ) AS count, p.post_title
     FROM posts p
     JOIN posts po
     GROUP BY p.post_title
     ORDER BY p.post_date DESC
     LIMIT 5
";

Important is to JOIN the same table, but name the table differently.重要的是 JOIN 同一个表,但表的名称不同。 Do not forget GROUP BY, must be used.不要忘记 GROUP BY,必须使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM