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
select mod(52,25) //Output is 2
select 24%7 //Output is 3
We can use mod function in our student table
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
Be the first to post comment on this article :
▼ More on Math functions in SQL 

 


OPINION POLL
What is the most important factor of a web site
