How can i select ODD or EVEN rows from a table?

SOLUTION 1

In order to select even or odd IDs from a MySQL table, you would use the ‘MOD’, this easy way to select the Odd and Even numbers.

Select Odd number

select column_name from table_name where mod(column_name,2)=1;

Select Even number

select column_name from table_name where mod(column_name,2)=0;

if you need an Odd number, put 1.

if you need an Even number, put 0.

 

SOLUTION 2

To select even or odd IDs from a MySQL table, you would use the modulo operator (like in PHP):

SELECT * FROM table WHERE (id % 2) = 0; # even
SELECT * FROM table WHERE (id % 2) > 0; # odd
Advertisements

Differences between Stored Procedures and Functions in MYSQL

  1. Stored Procedure can return zero or n values whereas function can return one value which is mandatory.
  2. Functions can be called from procedure whereas procedures cannot be called from function.
  3. Procedures can have input/output parameters for it whereas functions can have only input parameters.
  4. Procedure allows select as well as DML statement in it whereas function allows only select statement in it.
  5. Exception can be handled by try-catch block in a procedure whereas try-catch block cannot be used in a function.
  6. We can go for transaction management in procedure whereas we can’t go in function.
  7. Procedures can not be utilized in a select statement whereas function can be embedded in a select statement.