我想在查询中为3个变量添加别名,total_time_taken
和average
以及request_count
。
平均值是为了计算total_time_taken / request_count
以返回average
,但它会给我一个语法错误
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select(created_at, assigned_at, SUM(TIMESTAMPDIFF(SECOND, requests.created_at, r' at line 4
查询如下。
select
*,
COUNT(*) as request_count,
select(created_at, assigned_at, SUM(TIMESTAMPDIFF(SECOND, requests.created_at, requests.assigned_at)) from requests) as total_time_taken,
total_time_taken / request_count as average
from
`requests`
where
`deleted_at` is null
and
`submitted_at` >= '2017-03-30 00:00:00'
and
`requests`.`deleted_at` is null
group by
`engineer_id`
limit 5
答案 0 :(得分:1)
我没有看到子查询的重点,为什么不把每个工程师汇总的总和拿走?另外,你正在SELECT *
使用GROUP BY
,这通常不起作用,而且通常是错误的,因为它会包含非聚合列。相反,只需选择engineer_id
或其他列的汇总。
SELECT
engineer_id,
COUNT(*) AS request_count,
SUM(TIMESTAMPDIFF(SECOND, created_at, assigned_at)) AS total_time_taken,
SUM(TIMESTAMPDIFF(SECOND, created_at, assigned_at)) /
COUNT(*) AS average
FROM requests
WHERE deleted_at IS NULL AND
submitted_at >= '2017-03-30 00:00:00' AND
deleted_at IS NULL
GROUP BY engineer_id
LIMIT 5