I
I
Ilya Beloborodov2017-01-29 18:43:06
MySQL
Ilya Beloborodov, 2017-01-29 18:43:06

How to choose birthdays?

There is such a table - (date - birthday)

CREATE TABLE `birthday` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `date` datetime DEFAULT '0000-00-00 00:00:00',
  `name` tinytext,
  `photo` tinytext,
  `description` tinytext,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=241 DEFAULT CHARSET=cp1251;

How to choose the three nearest past and three nearest future? Only if there are birthday people in today, they also need to be selected. There are a lot of options on the Internet, but there are some nuances everywhere

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2017-01-29
@kowap

And there will be a lot of nuances. If, for example, seven people have birthdays today, which of them should be in the sample? And if all this is not taken into account, then it is easy

(SELECT *
  FROM `birthday`
  WHERE MONTH(`date`) > MONTH(CURDATE())
    OR (MONTH(`date`) = MONTH(CURDATE()) 
      AND DAYOFMONTH(`date`) = DAYOFMONTH(CURDATE()))
  ORDER BY MONTH(`date`), DAYOFMONTH(`date`)
  LIMIT 3)
UNION (SELECT *
  FROM `birthday`
  WHERE MONTH(`date`) = MONTH(CURDATE()) 
      AND DAYOFMONTH(`date`) = DAYOFMONTH(CURDATE()))
UNION (SELECT *
  FROM `birthday`
  WHERE MONTH(`date`) < MONTH(CURDATE())
    OR (MONTH(`date`) = MONTH(CURDATE()) 
      AND DAYOFMONTH(`date`) < DAYOFMONTH(CURDATE()))
  ORDER BY MONTH(`date`) DESC, DAYOFMONTH(`date`) DESC
  LIMIT 3)

A
Alexander Aksentiev, 2017-01-29
@Sanasol

date between date_sub(now(), interval N day) and  date_add(now(), interval N day)

What are the other nuances?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question