SQL MOD Command
Mod is a math function of MySQL which returns the reminder after a division. We will try to use this function in our query to get different results. Here are some examples.
select mod(100,20) // output is 0
We can use mod function in our student table
select mod(52,25) //Output is 2
select 24%7 //Output is 3
Getting all the records for which id number is even
SELECT * FROM `student` WHERE mod(id,2)=0
Another way we can also write
SELECT * FROM `student` WHERE id%2=0
How many even id numbers are there in our student table
SELECT count(id) as no, name,id FROM `student` group by mod(id,2)
Updating all the records with even number of student id by using concat function
update student set address = concat(name,'_address') where mod(id,2)=0
This article is written by plus2net.com team.
| ▼ More on Math functions in SQL|