how can i get row count values in mysql as @@ROWCOUNT in mssql
+3
A:
For SELECTs you can use the FOUND_ROWS
construct (documented here):
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
).
For UPDATE
/DELETE
/INSERT
, it's the ROW_COUNT construct
INSERT INTO your_table VALUES (1,2,3);
SELECT ROW_COUNT();
which will return the number of affected rows.
AndiDog
2010-02-09 13:13:21
@@ROWCOUNT helps in having the value after the select is executed - which is what i want; and then do further calculation in a stored procedure by taking the total number of rows returned
abs
2010-02-09 13:16:00
Sorry, must've mistaken that. Edited my answer.
AndiDog
2010-02-09 13:27:51
+1
A:
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.
mysql> SELECT SQL_CALC_FOUND_ROWS * FROM my_table;
mysql> SELECT FOUND_ROWS();
Yada
2010-02-09 13:13:45