我试图从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_updates
与ticketnumber
表格中的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
答案 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
";