Wednesday, August 28, 2013

One Query: Two output

Using "SQL_CALC_FOUND_ROWS" with SELECT STATEMENT

Example:-

mysql> select * from test;
+---------------------+--------+---------+----------+
| triggerDate         | Medium | Serious | Critical |
+---------------------+--------+---------+----------+
| 2012-12-20 00:00:00 |     10 |      20 |       30 |
| 2012-12-21 00:00:00 |     10 |      20 |       30 |
| 2012-12-22 00:00:00 |     10 |      20 |       30 |
| 2013-01-22 00:00:00 |     10 |      20 |       30 |
| 2013-02-22 00:00:00 |     10 |      20 |       30 |
| 2013-03-22 00:00:00 |     10 |      20 |       30 |
| 2013-03-22 00:00:00 |     10 |      20 |       30 |
| 2013-03-21 00:00:00 |     10 |      20 |       30 |
+---------------------+--------+---------+----------+
8 rows in set (0.00 sec)

mysql> SELECT SQL_CALC_FOUND_ROWS * FROM test limit 3;
+---------------------+--------+---------+----------+
| triggerDate         | Medium | Serious | Critical |
+---------------------+--------+---------+----------+
| 2012-12-20 00:00:00 |     10 |      20 |       30 |
| 2012-12-21 00:00:00 |     10 |      20 |       30 |
| 2012-12-22 00:00:00 |     10 |      20 |       30 |
+---------------------+--------+---------+----------+
3 rows in set (0.00 sec)

mysql> SELECT FOUND_ROWS();
+--------------+
| FOUND_ROWS() |
+--------------+
|            8 |
+--------------+
1 row in set (0.00 sec)

**
SQL_CALC_FOUND_ROWS and FOUND_ROWS() can be useful in situations when you want to restrict the number of rows that a query returns, but also determine the number of rows in the full result set without running the query again. An example is a Web script that presents a paged display containing links to the pages that show other sections of a search result. Using FOUND_ROWS() enables you to determine how many other pages are needed for the rest of the result. 

No comments:

Post a Comment