1
I set up a series of SQL but I’m having difficulty generating something that stress less the server, I ask help from you.
Follows the SQL:
SELECT '2015-06-10 00:00:00' AS 'Hora', COUNT(id_contato) AS 'Total' FROM contato WHERE data_hora LIKE '2015-06-10%' AND HOUR(data_hora) BETWEEN 00 AND 01;
SELECT '2015-06-10 01:00:00' AS 'Hora', COUNT(id_contato) AS 'Total' FROM contato WHERE data_hora LIKE '2015-06-10%' AND HOUR(data_hora) BETWEEN 01 AND 02;
SELECT '2015-06-10 02:00:00' AS 'Hora', COUNT(id_contato) AS 'Total' FROM contato WHERE data_hora LIKE '2015-06-10%' AND HOUR(data_hora) BETWEEN 02 AND 03;
SELECT '2015-06-10 03:00:00' AS 'Hora', COUNT(id_contato) AS 'Total' FROM contato WHERE data_hora LIKE '2015-06-10%' AND HOUR(data_hora) BETWEEN 03 AND 04;
SELECT '2015-06-10 04:00:00' AS 'Hora', COUNT(id_contato) AS 'Total' FROM contato WHERE data_hora LIKE '2015-06-10%' AND HOUR(data_hora) BETWEEN 04 AND 05;
SELECT '2015-06-10 05:00:00' AS 'Hora', COUNT(id_contato) AS 'Total' FROM contato WHERE data_hora LIKE '2015-06-10%' AND HOUR(data_hora) BETWEEN 05 AND 06;
SELECT '2015-06-10 06:00:00' AS 'Hora', COUNT(id_contato) AS 'Total' FROM contato WHERE data_hora LIKE '2015-06-10%' AND HOUR(data_hora) BETWEEN 06 AND 07;
SELECT '2015-06-10 07:00:00' AS 'Hora', COUNT(id_contato) AS 'Total' FROM contato WHERE data_hora LIKE '2015-06-10%' AND HOUR(data_hora) BETWEEN 07 AND 08;
SELECT '2015-06-10 08:00:00' AS 'Hora', COUNT(id_contato) AS 'Total' FROM contato WHERE data_hora LIKE '2015-06-10%' AND HOUR(data_hora) BETWEEN 08 AND 09;
SELECT '2015-06-10 09:00:00' AS 'Hora', COUNT(id_contato) AS 'Total' FROM contato WHERE data_hora LIKE '2015-06-10%' AND HOUR(data_hora) BETWEEN 09 AND 10;
SELECT '2015-06-10 10:00:00' AS 'Hora', COUNT(id_contato) AS 'Total' FROM contato WHERE data_hora LIKE '2015-06-10%' AND HOUR(data_hora) BETWEEN 10 AND 11;
SELECT '2015-06-10 11:00:00' AS 'Hora', COUNT(id_contato) AS 'Total' FROM contato WHERE data_hora LIKE '2015-06-10%' AND HOUR(data_hora) BETWEEN 11 AND 12;
SELECT '2015-06-10 12:00:00' AS 'Hora', COUNT(id_contato) AS 'Total' FROM contato WHERE data_hora LIKE '2015-06-10%' AND HOUR(data_hora) BETWEEN 12 AND 13;
SELECT '2015-06-10 13:00:00' AS 'Hora', COUNT(id_contato) AS 'Total' FROM contato WHERE data_hora LIKE '2015-06-10%' AND HOUR(data_hora) BETWEEN 13 AND 14;
SELECT '2015-06-10 14:00:00' AS 'Hora', COUNT(id_contato) AS 'Total' FROM contato WHERE data_hora LIKE '2015-06-10%' AND HOUR(data_hora) BETWEEN 14 AND 15;
SELECT '2015-06-10 15:00:00' AS 'Hora', COUNT(id_contato) AS 'Total' FROM contato WHERE data_hora LIKE '2015-06-10%' AND HOUR(data_hora) BETWEEN 15 AND 16;
SELECT '2015-06-10 16:00:00' AS 'Hora', COUNT(id_contato) AS 'Total' FROM contato WHERE data_hora LIKE '2015-06-10%' AND HOUR(data_hora) BETWEEN 16 AND 17;
SELECT '2015-06-10 17:00:00' AS 'Hora', COUNT(id_contato) AS 'Total' FROM contato WHERE data_hora LIKE '2015-06-10%' AND HOUR(data_hora) BETWEEN 17 AND 18;
SELECT '2015-06-10 18:00:00' AS 'Hora', COUNT(id_contato) AS 'Total' FROM contato WHERE data_hora LIKE '2015-06-10%' AND HOUR(data_hora) BETWEEN 18 AND 19;
SELECT '2015-06-10 19:00:00' AS 'Hora', COUNT(id_contato) AS 'Total' FROM contato WHERE data_hora LIKE '2015-06-10%' AND HOUR(data_hora) BETWEEN 19 AND 20;
SELECT '2015-06-10 20:00:00' AS 'Hora', COUNT(id_contato) AS 'Total' FROM contato WHERE data_hora LIKE '2015-06-10%' AND HOUR(data_hora) BETWEEN 20 AND 21;
SELECT '2015-06-10 21:00:00' AS 'Hora', COUNT(id_contato) AS 'Total' FROM contato WHERE data_hora LIKE '2015-06-10%' AND HOUR(data_hora) BETWEEN 21 AND 22;
SELECT '2015-06-10 22:00:00' AS 'Hora', COUNT(id_contato) AS 'Total' FROM contato WHERE data_hora LIKE '2015-06-10%' AND HOUR(data_hora) BETWEEN 22 AND 23;
SELECT '2015-06-10 23:00:00' AS 'Hora', COUNT(id_contato) AS 'Total' FROM contato WHERE data_hora LIKE '2015-06-10%' AND HOUR(data_hora) BETWEEN 23 AND 00;
The field
data_hora
is sweep?– rray
The reason for this
'2015-06-10 23:00:00' AS 'Hora'
? Wouldn’t it be better to take the date stored in the bank?– Guilherme Nascimento
is varchar, but is with the information in timestamp format Ex. "2015-06-09 08:15:02"
– MoisesGama
@Guilhermenascimento I did this because I need to create a display of quantity per hour, I mounted a for to generate the loop of each time.
– MoisesGama
Yes Moises, but this could be done in the PHP layer, you could also use only one
SELECT
andOR
and at the PHP layer organize the data. Surely this would optimize.– Guilherme Nascimento
@Guilhermenascimento I really tried to do but could not. I did a loop run to list. If you can help me be very grateful.
– MoisesGama
At the moment I can not for an example because I am on the mobile, as soon as I can.
– Guilherme Nascimento
@Guilhermenascimento Thank you, will help a lot.
– MoisesGama