There is no INTERSECT for MySQL and you needed to emulate using UNION ALL. This SQL at first glance seems weird, but it's not complicated, and goes through all the tests, finding the correct location for each time . It follows mass for the test, the logic and the tests themselves.
First a table was created that reproduces the one that was posted in the question.
CREATE TABLE 'LISTA_LOCAIS' (
'ID' int(11) NOT NULL,
'DATA_HORA' timestamp NOT NULL,
'LOCAL' varchar(100) NOT NULL,
'ENTRADA_SAIDA' varchar(10) NOT NULL
)
Then we create a test mass identical to the one shown in the question.
INSERT INTO LISTA_LOCAIS(ID,DATA_HORA,LOCAL,ENTRADA_SAIDA) VALUES(1,'2017-01-28 07:27','FABRICA' ,'Entrada');
INSERT INTO LISTA_LOCAIS(ID,DATA_HORA,LOCAL,ENTRADA_SAIDA) VALUES(2,'2017-01-28 09:00','FABRICA' ,'Saida' );
INSERT INTO LISTA_LOCAIS(ID,DATA_HORA,LOCAL,ENTRADA_SAIDA) VALUES(3,'2017-01-29 00:02','REVENDA 1' ,'Entrada');
INSERT INTO LISTA_LOCAIS(ID,DATA_HORA,LOCAL,ENTRADA_SAIDA) VALUES(4,'2017-01-29 04:00','REVENDA 1' ,'Saida' );
INSERT INTO LISTA_LOCAIS(ID,DATA_HORA,LOCAL,ENTRADA_SAIDA) VALUES(5,'2017-01-29 08:00','REVENDA 2' ,'Entrada');
INSERT INTO LISTA_LOCAIS(ID,DATA_HORA,LOCAL,ENTRADA_SAIDA) VALUES(6,'2017-01-29 10:00','REVENDA 2' ,'Saida' );
INSERT INTO LISTA_LOCAIS(ID,DATA_HORA,LOCAL,ENTRADA_SAIDA) VALUES(7,'2017-01-29 20:00','FABRICA' ,'Entrada');
INSERT INTO LISTA_LOCAIS(ID,DATA_HORA,LOCAL,ENTRADA_SAIDA) VALUES(8,'2017-01-29 23:00','FABRICA' ,'Saida' );
Now SQL to search the location at certain times:
SELECT LOCAL from (
(SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Entrada' AND 'SUA_TIMESTAMP_PESQUISADA' >= DATA_HORA ORDER BY DATA_HORA DESC LIMIT 2)
UNION ALL
(SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Saida' AND 'SUA_TIMESTAMP_PESQUISADA' <= DATA_HORA ORDER BY DATA_HORA ASC LIMIT 2)
) AS TABELA_UNIFICADA GROUP BY LOCAL HAVING count(*) >= 2
Explanation:
First it takes a part that marries with the entrance. Then take a part that marries the exit. It intersects the two. Note the balcony of the descending order for the entrance and ascending order for the exit to limit the records of this intersection well.
Follow the tests:
-- TESTE 1: REVENDA 1 em 2017-01-29 00:03:00
SELECT LOCAL from (
(SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Entrada' AND '2017-01-29 00:03:00' >= DATA_HORA ORDER BY DATA_HORA DESC LIMIT 2)
UNION ALL
(SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Saida' AND '2017-01-29 00:03:00' <= DATA_HORA ORDER BY DATA_HORA ASC LIMIT 2)
) AS TABELA_UNIFICADA GROUP BY LOCAL HAVING count(*) >= 2
-- TESTE 2: FABRICA em 2017-01-28 08:00
SELECT LOCAL from (
(SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Entrada' AND '2017-01-28 08:00' >= DATA_HORA ORDER BY DATA_HORA DESC LIMIT 2)
UNION ALL
(SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Saida' AND '2017-01-28 08:00' <= DATA_HORA ORDER BY DATA_HORA ASC LIMIT 2)
) AS TABELA_UNIFICADA GROUP BY LOCAL HAVING count(*) >= 2
-- TESTE 3: EM LUGAR NENHUM / CAMINHÃO EM ROTA em 2017-01-28 12:00 - não tem a entrada do local para esse período
SELECT LOCAL from (
(SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Entrada' AND '2017-01-28 12:00' >= DATA_HORA ORDER BY DATA_HORA DESC LIMIT 2)
UNION ALL
(SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Saida' AND '2017-01-28 12:00' <= DATA_HORA ORDER BY DATA_HORA ASC LIMIT 2)
) AS TABELA_UNIFICADA GROUP BY LOCAL HAVING count(*) >= 2
-- TESTE 4: REVENDA 2 em 2017-01-29 08:00
SELECT LOCAL from (
(SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Entrada' AND '2017-01-29 08:00' >= DATA_HORA ORDER BY DATA_HORA DESC LIMIT 2)
UNION ALL
(SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Saida' AND '2017-01-29 08:00' <= DATA_HORA ORDER BY DATA_HORA ASC LIMIT 2)
) AS TABELA_UNIFICADA GROUP BY LOCAL HAVING count(*) >= 2
-- TESTE 5: REVENDA 2 em 2017-01-29 09:00
SELECT LOCAL from (
(SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Entrada' AND '2017-01-29 09:00' >= DATA_HORA ORDER BY DATA_HORA DESC LIMIT 2)
UNION ALL
(SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Saida' AND '2017-01-29 09:00' <= DATA_HORA ORDER BY DATA_HORA ASC LIMIT 2)
) AS TABELA_UNIFICADA GROUP BY LOCAL HAVING count(*) >= 2
-- TESTE 6: REVENDA 2 em 2017-01-29 10:00
SELECT LOCAL from (
(SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Entrada' AND '2017-01-29 10:00' >= DATA_HORA ORDER BY DATA_HORA DESC LIMIT 2)
UNION ALL
(SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Saida' AND '2017-01-29 10:00' <= DATA_HORA ORDER BY DATA_HORA ASC LIMIT 2)
) AS TABELA_UNIFICADA GROUP BY LOCAL HAVING count(*) >= 2
-- TESTE 7: LUGAR NENHUM / CAMINHÃO EM ROTA em 2017-01-29 16:00
SELECT LOCAL from (
(SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Entrada' AND '2017-01-29 16:00' >= DATA_HORA ORDER BY DATA_HORA DESC LIMIT 2)
UNION ALL
(SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Saida' AND '2017-01-29 16:00' <= DATA_HORA ORDER BY DATA_HORA ASC LIMIT 2)
) AS TABELA_UNIFICADA GROUP BY LOCAL HAVING count(*) >= 2
-- TESTE 8: FABRICA em 2017-01-29 20:00
SELECT LOCAL from (
(SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Entrada' AND '2017-01-29 20:00' >= DATA_HORA ORDER BY DATA_HORA DESC LIMIT 2)
UNION ALL
(SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Saida' AND '2017-01-29 20:00' <= DATA_HORA ORDER BY DATA_HORA ASC LIMIT 2)
) AS TABELA_UNIFICADA GROUP BY LOCAL HAVING count(*) >= 2
-- TESTE 9: FABRICA em 2017-01-29 23:00
SELECT LOCAL from (
(SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Entrada' AND '2017-01-29 23:00' >= DATA_HORA ORDER BY DATA_HORA DESC LIMIT 2)
UNION ALL
(SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Saida' AND '2017-01-29 23:00' <= DATA_HORA ORDER BY DATA_HORA ASC LIMIT 2)
) AS TABELA_UNIFICADA GROUP BY LOCAL HAVING count(*) >= 2
-- TESTE 10: LUGAR NENHUM / CAMINHÃO EM ROTA em 2017-01-29 23:10
SELECT LOCAL from (
(SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Entrada' AND '2017-01-29 23:10' >= DATA_HORA ORDER BY DATA_HORA DESC LIMIT 2)
UNION ALL
(SELECT LOCAL FROM LISTA_LOCAIS WHERE
ENTRADA_SAIDA='Saida' AND '2017-01-29 23:10' <= DATA_HORA ORDER BY DATA_HORA ASC LIMIT 2)
) AS TABELA_UNIFICADA GROUP BY LOCAL HAVING count(*) >= 2