Well, come on.
First, your table has serious structuring problems, you do not keep (at least in this table) at any time what the customer is making the reservation, and also saves the final/start time and minute in 4 columns when you can easily use only 2 columns DATETIME
, then, restructuring her table, she would look like this:
+-----------------+--------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+-------------------+----------------+
| reserva_id | smallint(6) | NO | PRI | NULL | auto_increment |
| cliente | varchar(100) | NO | | NULL | |
| sala | smallint(3) | NO | | NULL |
| reservado_em | timestamp | NO | | CURRENT_TIMESTAMP | |
| datahora_inicio | datetime | NO | | NULL | |
| datahora_fim | datetime | NO | | NULL | |
+-----------------+--------------+------+-----+-------------------+----------------+
* Note that the field cliente
is text, ideal is you change this field to cliente_id
and set a foreign key.
Code CREATE
table:
CREATE TABLE `reservas` (
`reserva_id` SMALLINT(6) NOT NULL AUTO_INCREMENT,
`cliente` VARCHAR(100) NOT NULL,
`sala` SMALLINT(3) NOT NULL,
`reservado_em` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`datahora_inicio` DATETIME NOT NULL,
`datahora_fim` DATETIME NOT NULL,
PRIMARY KEY (`reserva_id`)
)
ENGINE=InnoDB
AUTO_INCREMENT=6
;
This way, you can register a reservation using:
insert into reservas(cliente, sala, datahora_inicio, datahora_fim) values('José', 001, '2015-05-25 11:00:00', '2015-05-25 11:59:59');
Then you can check if at the time chosen by the customer who is trying to make a reservation is already booked using:
select *
from reservas
where (
(datahora_inicio between '2015-05-25 11:00:00' and '2015-05-25 11:59:59') or
(datahora_fim between '2015-05-25 11:00:00' and '2015-05-25 11:59:59')
)
and sala = 1;
See the example in Sqlfiddle for you to test the queries.
Explain your situation better. Are you using any programming language to interact with the Bank? If so, which one? What environment? Detail your tools. And add more tags. I suggest a headline more in line with the question like, "How do I check for matching dates in the database?". It might attract more people.
– Guill
Opa the language that this interacting is php is a simple reservation system for example if there is already a reservation marked for the initial date the final date the initial time the final time and in the same then can not
– Admin
You repeated it three times what want to do (counting the comment above) not repeat so much. Explain one instead intelligibly. Add to the question the code you already have. What have you tried? Edit your question with the necessary information.
– Guill