dimanche 16 mai 2021

Select if, days to birthday

I recently started learning MySQL and I need to write a query using select if. The goal is to show the days to birthday for every person in the table.

So far I have the following code:

select datediff(date(concat(year(now()), '-', month(Person.Birthday), '-', day(Person.Birthday))),now()) as 'Days to Birthday'
from Person
where datediff(
date(concat(year(now()), '-',month(Person.Birthday), '-',
day(Person.Birthday))),now()) >= 0;

So far so good. This works and I also have the version where the results are < 0.

How can I integrate these lines in the select if statement? I tried for the last two days, but I wasn't successful.

Also, I tried to get a result using case and where, but I had no luck either.

I would be happy if anyone could offer a solution!

Aucun commentaire:

Enregistrer un commentaire