Mysql --found_rows()与row_count()

found_rows()函数

返回上一条sql所返回的行数
文档地址:http://dev.mysql.com/doc/refman/5.6/en/information-functions.html#function_found-rows

mysql> select count(*) from incr_order;
+----------+
| count(*) |
+----------+
|        7 |
+----------+
1 row in set (0.00 sec)

mysql> select * from incr_order;
+------+
| id   |
+------+
|    3 |
|    1 |
|    5 |
|    4 |
|    9 |
|    0 |
| NULL |
+------+
7 rows in set (0.00 sec)

mysql> select found_rows();
+--------------+
| found_rows() |
+--------------+
|            7 |
+--------------+
1 row in set (0.00 sec)

当有sql_calc_found_rows时,即使有空值,也返回真实行数,而非网上所说的减去null值

mysql> select sql_calc_found_rows * from incr_order;
+------+
| id   |
+------+
|    3 |
|    1 |
|    5 |
|    4 |
|    9 |
|    0 |
| NULL |
+------+
7 rows in set (0.00 sec)

mysql> select found_rows();
+--------------+
| found_rows() |
+--------------+
|            7 |
+--------------+
1 row in set (0.00 sec)

mysql> select count(id) from incr_order;
+-----------+
| count(id) |
+-----------+
|         6 |
+-----------+
1 row in set (0.00 sec)

mysql> select * from incr_order limit 5;
+------+
| id   |
+------+
|    3 |
|    1 |
|    5 |
|    4 |
|    9 |
+------+
5 rows in set (0.00 sec)

mysql> select found_rows();
+--------------+
| found_rows() |
+--------------+
|            5 |
+--------------+
1 row in set (0.00 sec)

当有limit且有sql_calc_found_rows时,found_rows()返回表总行数,而非limit后所返回行数

mysql> select sql_calc_found_rows * from incr_order limit 5;
+------+
| id   |
+------+
|    3 |
|    1 |
|    5 |
|    4 |
|    9 |
+------+
5 rows in set (0.00 sec)

mysql> select found_rows();
+--------------+
| found_rows() |
+--------------+
|            7 |
+--------------+
1 row in set (0.00 sec)

limit带偏移量时

mysql> select * from incr_order limit 2,2;
+------+
| id   |
+------+
|    5 |
|    4 |
+------+
2 rows in set (0.00 sec)

这个found_rows() 返回4行,因为他从变偏移2的地方再扫描2行,所以返回4
mysql> select found_rows();
+--------------+
| found_rows() |
+--------------+
|            4 |
+--------------+
1 row in set (0.00 sec)

这个由于where id=100 为空,所以后面的limit根本没执行
mysql> select * from incr_order where id=100 limit 2,2;
Empty set (0.00 sec)

mysql> select found_rows();
+--------------+
| found_rows() |
+--------------+
|            0 |
+--------------+
1 row in set (0.00 sec)

有sql_calc_found_rows还是返回总行数
mysql> select sql_calc_found_rows * from incr_order limit 2,2;
+------+
| id   |
+------+
|    5 |
|    4 |
+------+
2 rows in set (0.00 sec)

mysql> select found_rows();
+--------------+
| found_rows() |
+--------------+
|            7 |
+--------------+
1 row in set (0.00 sec)

row_count()函数

row_count()函数一般用于返回被update,insert,delete实际修改的行数。
文档地址:http://dev.mysql.com/doc/refman/5.6/en/information-functions.html#function_row-count

In MySQL 5.6, ROW_COUNT() returns a value as follows:

DDL statements: 0. This applies to statements such as CREATE TABLE or DROP TABLE.

DML statements other than SELECT: The number of affected rows. This applies to statements such as UPDATE, INSERT, or DELETE (as before), but now also to statements such as ALTER TABLE and LOAD DATA INFILE.

SELECT: -1 if the statement returns a result set, or the number of rows “affected” if it does not. For example, for SELECT * FROM t1, ROW_COUNT() returns -1. For SELECT * FROM t1 INTO OUTFILE ‘file_name’, ROW_COUNT() returns the number of rows written to the file.

SIGNAL statements: 0.

For UPDATE statements, the affected-rows value by default is the number of rows actually changed. If you specify the CLIENT_FOUND_ROWS flag to mysql_real_connect() when connecting to mysqld, the affected-rows value is the number of rows “found”; that is, matched by the WHERE clause.

也就是说对于update语句,row_count() 默认返回的是实际被修改的行数;但是通过参数设置,也可以返回找到的行数(或者说匹配的行数,受影响的行数),这样设置就能兼容于Oracle ps/sql中 sql%rowcount 和 sql server 中的 @@RowCount。

mysql> select * from incr_order;
+------+
| id   |
+------+
|    3 |
|    1 |
|    5 |
|    4 |
|    9 |
|    0 |
| NULL |
+------+
7 rows in set (0.00 sec)

mysql> delete from incr_order where id=100;
Query OK, 0 rows affected (0.00 sec)

mysql> select row_count();
+-------------+
| row_count() |
+-------------+
|           0 |
+-------------+
1 row in set (0.00 sec)

mysql> update incr_order set id=8 where id<5;
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

mysql> select row_count();
+-------------+
| row_count() |
+-------------+
|           4 |
+-------------+
1 row in set (0.00 sec)

mysql> insert into incr_order values(1),(4),(0),(3);
Query OK, 4 rows affected (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select row_count();
+-------------+
| row_count() |
+-------------+
|           4 |
+-------------+
1 row in set (0.00 sec)

mysql> select * from incr_order;
+------+
| id   |
+------+
|    8 |
|    8 |
|    5 |
|    8 |
|    9 |
|    8 |
| NULL |
|    1 |
|    4 |
|    0 |
|    3 |
+------+
11 rows in set (0.00 sec)

mysql> select row_count();
+-------------+
| row_count() |
+-------------+
|          -1 |
+-------------+
1 row in set (0.00 sec)

你可能感兴趣的:(Mysql --found_rows()与row_count())