PHP选择2个日期之间的平均值

时间:2013-09-09 23:47:54

标签: php mysql sql

我试图从2个不同的表中选择php中2个日期之间的平均时间。

我有这个:

$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";

但我希望能够从datetime表格中的tickets列和startdate表格中的ticket_updates列计算平均“响应”时间

ticket_seq表格中的ticket_updatesticketnumber表格中的tickets列相关联,因此我需要选择从ticket_updates创建的第一行与tickets

中所选行链接的表

我试过了:

$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";

但没有返回任何内容,当我在mysql中运行时,我得到:

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

1 个答案:

答案 0 :(得分:0)

我已设法使用此查询解决此问题:

$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
";