PHP Seleccionar promedio entre 2 fechas

i am trying to select the average time between 2 dates in php from 2 different tables.

tengo esto:

$sql="select *, avg(end_date - start_date) as avg_days from tickets where deleted = '' and DAY(datetime) = '04' and MONTH(datetime) = '".date("m")."' and YEAR(datetime) = '".date("Y")."' group by assigned_to";

but i want to be able to work out the average 'response' time from the datetime columna en el tickets mesa y el startdate columna en el ticket_updates mesa.

al ticket_seq en el capítulo respecto a la ticket_updates table links with the ticketnumber columna en el tickets table so i need to select the first row created from the ticket_updates table that links with the selected row from the tickets mesa

Yo he tratado:

$sql="select tickets.*, avg(tickets.datetime - ticket_updates.timestart) as avg_days from tickets, ticket_updates where tickets.ticketnumber = ticket_updates.ticket_seq and deleted = '' and DAY(datetime) = '04' and MONTH(datetime) = '".date("m")."' and YEAR(datetime) = '".date("Y")."' group by assigned_to";

but nothing is returned and when i run in mysql i get:

 #1052 - Column 'datetime' in where clause is ambiguous

preguntado el 10 de septiembre de 13 a las 00:09

What's the part causing you difficulty? -

see my question edit.. -

The error message means you have to qualify the field datetime with one of the tables in your where clause. You know how to do that I trust. -

im not too sure how to do that? -

1 Respuestas

I have managed to fix the issue by using this query:

$sql="
SELECT 
    tickets.*, 
    avg(tickets.datetime - ticket_updates.timestart) as avg_days 
FROM 
    tickets, ticket_updates 
WHERE 
    tickets.ticketnumber = ticket_updates.ticket_seq 
AND deleted = '' 
AND DAY(tickets.datetime) = '04' 
AND MONTH(tickets.datetime) = '".date("m")."' 
AND YEAR(tickets.datetime) = '".date("Y")."' 
GROUP BY 
    tickets.assigned_to
";

Respondido el 10 de Septiembre de 13 a las 01:09

Accept your own answer if it fixed the issue. - SamV

i would love to but i cant for 2 days - charlie

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.