How can we fetch alternate odd numbered records from MySQL table?

MySQLMySQLi Database

To understand this concept, we are using the data from table ‘Information’ as follows −

mysql> Select * from Information;
+----+---------+
| id | Name    |
+----+---------+
| 1  | Gaurav  |
| 2  | Ram     |
| 3  | Rahul   |
| 4  | Aarav   |
| 5  | Aryan   |
| 6  | Krishan |
+----+---------+
6 rows in set (0.00 sec)

Now, the query below will fetch the alternate odd-numbered records from the above table ‘Information’ −

mysql> Select id,Name from information group by id having mod(id,2) = 1;
+----+--------+
| id | Name   |
+----+--------+
| 1  | Gaurav |
| 3  | Rahul  |
| 5  | Aryan  |
+----+--------+
3 rows in set (0.09 sec)
raja
Published on 22-Feb-2018 12:37:01
Advertisements