tags:

views:

30

answers:

2

I'm looking for the built-in MySQL information function that returns the number of rows just selected, like ROW_COUNT() does for INSERT, UPDATE, etc.

In a stored procedure I wish to do this:

...
select episode_name from bestLOSTepisodes
set _episodes_found = SELECTED_ROWS();
...

I've done a fair amount of digging and have been unable to find such a function like SELECTED_ROWS().

+1  A: 

You may want to use the FOUND_ROWS() function.

Example:

CREATE TABLE users (id int, name varchar(20), age int);

INSERT INTO users VALUES (1, 'Bob', 19);
INSERT INTO users VALUES (2, 'Jack', 25);
INSERT INTO users VALUES (3, 'Paul', 15);
INSERT INTO users VALUES (4, 'Steve', 35);
INSERT INTO users VALUES (5, 'Mark', 17);

SELECT * FROM users WHERE age > 18;
+------+-------+------+
| id   | name  | age  |
+------+-------+------+
|    1 | Bob   |   19 |
|    2 | Jack  |   25 |
|    4 | Steve |   35 |
+------+-------+------+
3 rows in set (0.00 sec)

SELECT FOUND_ROWS() as num_of_rows_found;
+-------------------+
| num_of_rows_found |
+-------------------+
|                 3 |
+-------------------+
1 row in set (0.00 sec)
Daniel Vassallo
Thanks! I just discovered this works even without a LIMIT.
Pete Alvin
@Pete Alvin: Yes, you can use the `SQL_CALC_FOUND_ROWS` to return the number of rows the server would have returned without the `LIMIT`. 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.
Daniel Vassallo
+1  A: 

You have to FOUND_ROWS with SQL_CALC_FOUND_ROWS in your query : http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows

MatTheCat