Mysql query ranges

Asked

Viewed 201 times

3

I need to assemble a select, where I need to display the results outside a code range. For example: I have code 2 as the beginning and code 6 as the end, how could I not display the codes 3, 4 and 5 and display only code 1 and 5 onwards? Only with SQL is possible?

With this select I can not display the initial and final code, but still showing what is between them

SELECT h.cod, h.horario FROM horarios h 
WHERE h.cod NOT IN (SELECT cod_hora_saida FROM reservas) AND 
      h.cod NOT IN (SELECT cod_hora_prevista FROM reservas) 
ORDER BY h.cod ASC

UPDATE

I keep trying, but I still could not show the result I need. The tables I use are the 2 below inserir a descrição da imagem aqui inserir a descrição da imagem aqui

The result I’m trying is this, using the tables below as an example:

06:00 06:30 07:00 07:30 08:00 08:30 09:00 09:30 10:00 13:00 13:30 14:00 14:30 15:00 18:00 18:30 19:00

LAST UPDATE

I tested Union all function, with a reservation worked, but if you have more than one on the same day no longer works. Suggestions?

select * from horarios where cod < 20 union all select * from horarios where cod > 25

  • What have you ever tried to do?

  • Have tried SELECT * FROM table Where code >= 1 and code < 2 and code >= 5?

  • Yes, he keeps showing

  • edited the question with the last attempts I made.

  • In your first query take the AND and put a OR. Post the result!!!

  • No, it brought as a result the entire schedule table...

Show 1 more comment

1 answer

1


If someone goes through a similar problem, the solution was this:

SELECT * FROM HORARIO H
 WHERE NOT EXISTS (
SELECT * FROM RESERVA R
 WHERE H.COD BETWEEN R.SAIDA AND R.RETORNO-1)
ORDER BY H.COD

Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.