How to select a range between dates to avoid an SQL insert?

0

I have a table called etapa which has id_etapa, etapa_inicial, etapa_final ... what statement (sql) can I make to show the stages that are between the etapa_inicial and the etapa_final ?
since the idea is that if the following stage was created: ..

EJ:

etapa_inicial: 28/03/2017
etapa_final: 30/04/2017

When you redo a insert do not insert etapas that are within that existing range ...

I hope you made me understand and I would appreciate the collaboration.

    
asked by JDavid 28.03.2017 в 17:51
source

2 answers

1
SELECT * FROM etapa
WHERE (etapa_inicial >= '28/03/2017' AND etapa_inicial <= '30/04/17')
OR (etapa_final >= '28/03/2017' AND etapa_final <= '30/04/17');

If what you want to know is only if you insert you can change the * for a COUNT(*) and in your program if it is more than one the result of the query does not insert it

    
answered by 28.03.2017 в 17:58
0

You can use the BETWEEN option.

Example:

$query="SELECT * FROM etapa where fecha BETWEEN '$fecha_1' AND '$fecha_2' ORDER BY id_etapa DESC";

... and then get the id_aporte within this range to specify in another filter:

select * FROM etapa where id_etapa !="$id_etapa".
    
answered by 16.05.2017 в 00:40