Mysql 是否与 mssql 中的 @@ROWCOUNT 等效?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2229218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does Mysql have an equivalent to @@ROWCOUNT like in mssql?
提问by abs
How can I get row count values in MySQL as @@ROWCOUNT
does in mssql?
如何像@@ROWCOUNT
在 mssql 中一样在 MySQL 中获取行计数值?
回答by AndiDog
For SELECTs you can use the FOUND_ROWS
construct (documented here):
对于 SELECT,您可以使用FOUND_ROWS
构造(此处记录):
SELECT SQL_CALC_FOUND_ROWS something FROM your_table WHERE whatever;
SELECT FOUND_ROWS( ) ;
which will return the number of rows in the last SELECT
query (or if the first query has a LIMIT
clause, it returns the number of rows there would've been without the LIMIT
).
这将返回最后一个SELECT
查询中的行数(或者如果第一个查询有一个LIMIT
子句,它将返回没有LIMIT
.的行数)。
For UPDATE
/DELETE
/INSERT
, it's the ROW_COUNTconstruct
对于UPDATE
/ DELETE
/ INSERT
,它的ROW_COUNT结构
INSERT INTO your_table VALUES (1,2,3);
SELECT ROW_COUNT();
which will return the number of affected rows.
这将返回受影响的行数。
回答by Lazarus
回答by Yada
The simplest way would be to use a variable:
最简单的方法是使用变量:
mysql> SELECT @rowcount:=COUNT(*) FROM my_table;
mysql> SELECT @rowcount;
Or you can use FOUND_ROWS()
construct after putting a SQL_CALC_FOUND_ROWS in a SELECT statement.
或者您可以FOUND_ROWS()
在 SELECT 语句中放入 SQL_CALC_FOUND_ROWS 后使用构造。
mysql> SELECT SQL_CALC_FOUND_ROWS * FROM my_table;
mysql> SELECT FOUND_ROWS();
回答by Rolf
There is another way:
还有一种方法:
CREATE TEMPORARY TABLE `results` AS ( *** Your query without LIMIT *** );
Get the row count
获取行数
SELECT COUNT(*) FROM `results`;
Get your subset
获取您的子集
SELECT * FROM `results` LIMIT 5,10;
The temporary table exists only in the current session. I would still clean-up afterwards
临时表只存在于当前会话中。之后我仍然会清理
DROP TEMPORARY TABLE `results`;