0

我正在尝试从 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计算出平均“响应”时间。ticketsstartdateticket_updates

ticket_seq表中的 与表中的列ticket_updates链接,因此我需要选择从表中创建的第一行,该表与表中的选定行链接ticketnumberticketsticket_updatestickets

我努力了:

$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
4

1 回答 1

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
";
于 2013-09-10T00:03:19.337 回答