MYSQL query with range of Dates of an id


I'm doing a query on mysql, which should make me based on a given id and a range of dates, bring me the records between those dates. I'm starting to learn mysql so the most sure thing is that I'm formulating everything wrong.

This is being implemented in a C # program. That part is already there and everything works, the only thing is that I do not know how to make the consulate, it occurred to me also to only perform the query of the id and through c # filter the dates of the DateTimePicker, but I do not think it's optimal if I can directly do this in the mysql query.

and tried this but it does not work:

select * from registro_ausentismos where id = '381' 
AND (fecha_inicio between >= '2018-09-10'
AND fecha_final between <= '2018-09-30')

More Info:

I think that with the following image I can understand a little more:

There we see that what I do is ask for an employee code and a range of dates, I need to show the records between the selected dates, I do not know if that filter can be made directly from a MySQL consulate.

    CREATE TABLE 'registro_ausentismos' (
    'id' INT(11) NOT NULL,
    'nombre' VARCHAR(250) NOT NULL,
    'apellidos' VARCHAR(250) NOT NULL,
    'tipo_documento' VARCHAR(50) NOT NULL,
    'numero_documento' BIGINT(20) NOT NULL,
    'eps' VARCHAR(250) NOT NULL,
    'cda' VARCHAR(50) NOT NULL,
    'cargo' VARCHAR(50) NOT NULL,
    'numero_incapacidad' INT(11) NOT NULL AUTO_INCREMENT,
    'tipo_evento' VARCHAR(50) NOT NULL DEFAULT '0',
    'dias_incapacidad' INT(11) NOT NULL DEFAULT '0',
    'fecha_inicio' DATE NOT NULL,
    'fecha_final' DATE NOT NULL,
    'codigo_diagnostico' VARCHAR(50) NOT NULL DEFAULT '0',
    'valor_incapacidad' INT(10) NULL DEFAULT '0',
    PRIMARY KEY ('numero_incapacidad', 'id')

and that is the table where the records are being stored.

asked by Andres Rodriguez FARP 08.10.2018 в 17:23

1 answer


You have a syntax error in your query, it should be like that;

SELECT * FROM registro_ausentismos 
WHERE id = '381' 
AND fecha_inicio BETWEEN '2018-09-10' AND '2018-09-30'
AND fecha_final BETWEEN '2018-09-10' AND '2018-09-30'

I'll explain,

When you make queries between date ranges using BETWEEN you just have to tell them the 2 dates separated by AND without any operator, since BETWEEN takes care of everything.

answered by 08.10.2018 / 17:37