MySQL query to select too many rows?

MySQLMySQLi Database

You can use LIMIT for this, which is used to fetch limited number of records. Let us first create a table −

mysql> create table DemoTable
(
   Id int,
   Name varchar(20)
);
Query OK, 0 rows affected (0.53 sec)

Insert records in the table using insert command −

mysql> insert into DemoTable values(10,'John');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values(11,'Chris');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values(12,'David');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values(13,'Carol');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values(14,'Mike');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values(15,'Sam');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable values(16,'Robert');
Query OK, 1 row affected (0.14 sec)

Display records from the table using select command −

mysql> select *from DemoTable;

This will produce the following output −

+------+--------+
| Id   | Name   |
+------+--------+
| 10   | John   |
| 11   | Chris  |
| 12   | David  |
| 13   | Carol  |
| 14   | Mike   |
| 15   | Sam    |
| 16   | Robert |
+------+--------+
7 rows in set (0.00 sec)

Now let us select too many rows. The below query will skip first 2 rows and return 4 rows −

mysql> select *from DemoTable limit 2,4;

This will produce the following output −

+------+-------+
| Id   | Name  |
+------+-------+
| 12   | David |
| 13   | Carol |
| 14   | Mike  |
| 15   | Sam   |
+------+-------+
4 rows in set (0.00 sec)
raja
Published on 29-Apr-2019 11:59:37
Advertisements