33 Votes

Using MySQL as a Calculator

Tip by Computer Expert | Last update on 2023-01-29 | Created on 2014-03-11

Today, I would like to say a few words about the possibilities you have when using MySQL. It is much more than just doing queries to extract some data from a database.

For example, you can misuse MySQL as a calculator without even accessing any database. The following MySQL commands are showing some examples:

SELECT 1+1;
SELECT 2-1;
SELECT 3*3;
SELECT (1+2)*(3+4);

But not only calculating with numbers is possible in MySQL. Likewise it is possible to calculate with dates and times using the function DATE_ADD().

SELECT DATE_ADD(CURRENT_DATE(), INTERVAL 10 DAY);
SELECT DATE_ADD(CURRENT_DATE(), INTERVAL -1 WEEK);
SELECT DATE_ADD(CURRENT_DATE(), INTERVAL 10 HOUR);

With CURRENT_DATE() you are determining the current date and in the example, we are adding three different periods of time to it. By adding "10 DAY", we get the day that is 10 days in future. With "-1 WEEK", we can retrieve the day one week before and with "10 HOUR", we get the point in time 10 hours forward.

Other possible values are MICROSECOND, SECOND, MINUTE, HOUR, DAY, WEEk, MONTH, QUARTER and YEAR.

Of course, this function is useful for many applications. For instance, if you would like to write a deadline into your database or if you would like to show all comments or new entries of the last week.

ReplyPositiveNegative

About the Author

AvatarThe author has not added a profile short description yet.
Show Profile

 

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.