gpt4 book ai didi

mysql - MySQL 查询中的子选择

转载 作者:行者123 更新时间:2023-11-29 10:54:51 25 4
gpt4 key购买 nike

我想在查询中为 3 个变量添加别名:total_time_takenaveragerequest_count

平均值旨在计算total_time_taken/request_count以返回平均值,但它给了我一个语法错误

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

最佳答案

我不明白子查询的意义,为什么不直接取每个工程师聚合的总和呢?另外,您正在使用 GROUP BY 执行 SELECT *,这通常不起作用,而且通常是错误的,因为它会包含非聚合列。相反,只需选择 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

关于mysql - MySQL 查询中的子选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43180603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com