Home > mysql, PHP > Calculations ? Try and use SQL

Calculations ? Try and use SQL

Hmm.. yes.. I literally meant it.. for several things you can directly use SQL calculations, though the queries and functions shown here are tested only with mysql, it should work with most other sql servers too. The cliche is if you have the sql server on the same server where your webserver and php interpreter is running. In simple terms, if you connect to mysql on localhost, sql can be used to a great extend to do calculations.

Some time back I did read on some other site, where two dates where calculated for the current month, using php mktime and date functions, to be passed in an sql query, for a range selection. I am not for an argument, but it seems more efficient to use the date calculations directly in the sql query, than pre calculating this unless otherwise it is needed to be printed in the output. Even then, the calculation can be done by the sql, and the result returned.

SQL calculations along with SQL session variables, will help a programmer achieve a very high optimization level for the scripts, thereby reducing webserver, and interpreter load.

Date calculations can use date_format, date_sub, unix_timestamp and from_unixtime and other related functions. Aggregates has all complimentary functions like sum, abs and others. By using a combination of these, any calculation can be achieved by sending an sql query.

Categories: mysql, PHP Tags:
  1. No comments yet.
  1. No trackbacks yet.

− two = two