William Estep

My journey to retire and sail. Writing, reading, technology, programming, photography, coffee, equality, sailing, and the oxford comma.

Common MySQL Queries

Category: Programming
Tags: technologymysql

I ran across this terrific resource, Common MySQL Queries.

Tons of great examples...for example:

Age in years

Given a birthdate in @dob, here are two simple formulae for age in years:

Date_format( From_Days( To_Days(Curdate()) - To_Days(@dob) ), '%Y' ) + 0

Year(Curdate()) - Year(@dob) - ( Right(Curdate(),5) < Right(@dob,5) )

and here is one for age in years to two decimal places, ignoring day of month:

Round((((Year(now()) - Year(@dob)))*12 + (((Month(now()) - Month(@dob)))))/12, 2)


Similar Posts

© William Estep