Yes, you might be able to do it in SQL, but the code might become complicated. Is there any specific reason that you want an SQL on this one?
I would recommend any server-side language such as
PHP, like Healdem also said.
_________________
Infrared Sauna