简体   繁体   English

帮助计算总行数

[英]Help counting total number of rows

I have the following type of query: 我有以下类型的查询:

SELECT * FROM TABLE
GROUP BY Table.Table_ID
LIMIT 10

I want to know how many rows would be selected if that LIMIT 10 weren't there. 我想知道如果没有LIMIT 10 ,将会选择多少行。 I can't simply select COUNT(Table_ID) because I group by that, so it will give 1 in every row for that. 我不能简单地选择COUNT(Table_ID)因为我将其分组,因此它将在每一行中给出1。

Clearly described in the manual : 手册中有明确说明

SQL_CALC_FOUND_ROWS tells MySQL to calculate how many rows there would be in the result set, disregarding any LIMIT clause. SQL_CALC_FOUND_ROWS告诉MySQL忽略任何LIMIT子句,计算结果集中将有多少行。 The number of rows can then be retrieved with SELECT FOUND_ROWS() . 然后可以使用SELECT FOUND_ROWS()检索行数。 See Section 11.13, “Information Functions”. 请参见第11.13节“信息功能”。

If you follow the link to Section 11.13, there's then an example: 如果您单击第11.13节的链接,则有一个示例:

FOUND_ROWS()

A SELECT statement may include a LIMIT clause to restrict the number of rows the server returns to the client. SELECT语句可以包含LIMIT子句,以限制服务器返回给客户端的行数。 In some cases, it is desirable to know how many rows the statement would have returned without the LIMIT , but without running the statement again. 在某些情况下,希望知道在没有LIMIT情况下该语句将返回多少行,而无需再次运行该语句。 To obtain this row count, include a SQL_CALC_FOUND_ROWS option in the SELECT statement, and then invoke FOUND_ROWS() afterward: 要获得此行计数,请在SELECT语句中包括一个SQL_CALC_FOUND_ROWS选项,然后再调用FOUND_ROWS()

 mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name -> WHERE id > 100 LIMIT 10; mysql> SELECT FOUND_ROWS(); 

The second SELECT returns a number indicating how many rows the first SELECT would have returned had it been written without the LIMIT clause. 第二个SELECT返回一个数字,该数字指示如果没有LIMIT子句而写入的第一个SELECT将返回多少行。

In the absence of the SQL_CALC_FOUND_ROWS option in the most recent successful SELECT statement, FOUND_ROWS() returns the number of rows in the result set returned by that statement. 如果在最近成功执行的SELECT语句中没有SQL_CALC_FOUND_ROWS选项,则FOUND_ROWS()返回该语句返回的结果集中的行数。 If the statement includes a LIMIT clause, FOUND_ROWS() returns the number of rows up to the limit. 如果该语句包含LIMIT子句,则FOUND_ROWS()返回不超过限制的行数。 For example, FOUND_ROWS() returns 10 or 60 , respectively, if the statement includes LIMIT 10 or LIMIT 50, 10 . 例如,如果语句包含LIMIT 10LIMIT 50, 10 FOUND_ROWS() ,则FOUND_ROWS()返回1060

Please, use the documentation as your first port of call. 请使用文档作为您的第一个停靠港。

SELECT COUNT(DISTINCT TABLE_ID) FROM TABLE

You could call the mysql_num_rows function on a result set containing all the rows to find out. 您可以在包含所有要查找的行的结果集上调用mysql_num_rows函数。

$result = mysql_query('SELECT * FROM TABLE GROUP BY Table.Table_ID');
$row_count = mysql_num_rows($result);

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

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