Imagine that there is such a base

create table monthes(month_id int, date datetime); insert into monthes values(1,'2016-08-01 00:00:00') ,(2,'2016-09-01 00:00:00') ,(3,'2016-06-01 00:00:00') ,(4,'2016-07-01 00:00:00') ; 

and there is an array (in php)

 $array = ['september', 'august']; 

How can you select the first and second entry in this array?

here are my "attempts".

 select * from monthes where DATENAME(month, `monthes.date` ) = 'september' 

sqlfiddle

  • I want to go through IN somehow, but I don’t know how - Igor Kalamurda
  • And which DBMS is used? What features can I use? Or do you need a cross-solution that will work in all DBMS? - cheops
  • mysql, you already answered thank you)) - Igor Kalamurda

1 answer 1

In various DBMS different mechanisms and functions for working with calendar data. More or less compact solution depends on the specific dialect. For example, in the case of MySQL DBMS, you can use the DATE_FORMAT() function, which allows you to extract the name of the full month name. To retrieve records for a group of months, you can use the following query.

 SELECT * FROM `monthes` WHERE DATE_FORMAT(`date`, '%M') IN ('september', 'august') 

For a single month, the query may look like this.

 SELECT * FROM `monthes` WHERE DATE_FORMAT(`date`, '%M') = 'september'