3 3 Votes

Using MySQL as a Calculator

Tip by Computer Expert | 11/03/2014 at 17:01

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

Avatar AuthorThe author has not added a short description to his profile yet.
Show Profile | Message

 

Related Topics

PHP: Determine Week Number
Tutorial | 0 Comments

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 articles on askingbox.com. How to do.