Use BETWEEN together with IN ()

-1

I would like to compare several dates within a date range. I tried to perform the query below but I can not get a proper logic.

Query with Array + BETWEEN in Mysql, or rather use the expression IN() along with BETWEEN .

I have tried some forms but can not

1)

SELECT
  *
FROM
  Conteudo
WHERE
  ('2018-03-22', '2018-03-23') BETWEEN DATE(DataInicio) AND DATE(DataFim)

2)

SELECT
      *
    FROM
      Conteudo
    WHERE
      BETWEEN DATE(DataInicio) AND DATE(DataFim) IN('2018-03-22','2018-03-23' )
    
asked by anonymous 22.03.2018 / 18:09

3 answers

2

It is "possible", but to do this you need to isolate the expressions:

SELECT
    *
FROM
    Conteudo
WHERE
    campo_data BETWEEN DATE(DataInicio) AND DATE(DataFim) 
    AND campo_data IN('2018-03-22','2018-03-23')
    
22.03.2018 / 18:34
1

Well, as far as I know this is not possible, but you can add OR conditions in your code instead of IN, so I'd look something like this:

SELECT
  *
FROM
  Conteudo
WHERE
  '2018-03-22' BETWEEN DATE(DataInicio) AND DATE(DataFim)
  OR '2018-03-23' BETWEEN DATE(DataInicio) AND DATE(DataFim)
...

Follow the example with tests:

CREATE TABLE teste_datas(
    descricao VARCHAR(255), 
    data_inicio DATE, 
    data_fim DATE
);

INSERT INTO teste_datas(descricao, data_inicio, data_fim)
VALUES('desc 1', '2018-03-01', '2018-03-22'),
('desc 1', '2018-03-01', '2018-03-23'),
('desc 2', '2018-03-01', '2018-03-24'),
('desc 3', '2018-03-01', '2018-03-08'),
('desc 4', '2018-03-01', '2018-03-02');'

If I run this query :

SELECT
    *
FROM teste_datas
WHERE '2018-03-22' BETWEEN data_inicio AND data_fim;

The result will be desc1, desc2 e desc3 since the date I'm looking for fits between the two periods

SELECT*FROMteste_datasWHERE'2018-03-22'BETWEENdata_inicioANDdata_fimOR'2018-03-07'BETWEENdata_inicioANDdata_fim;

IfIaddORtoqueryitwillreturndesc1,desc2,desc3edesc4becausetheseconddatealsofitsinarow.

    
22.03.2018 / 18:15
-1

It is possible, as follows.

The only however is if you want to var a OR as in the example you downloaded or a AND .

SELECT * FROM Conteudo
WHERE (campo_data BETWEEN DataInicio AND DataFim)
OR (campo_data in ('2018-03-22','2018-03-23'))
    
22.03.2018 / 19:35