我有两个表features
和responses
,其中每个功能可能有多个响应(或者可能是零响应)。响应表有一个包含时间戳的began
andended
列(至少会有一个开始时间戳,结束可能为 NULL)。
我正在尝试提取features
他们各自响应的摘要详细信息列表(如果有)。我没有在responses
表格上使用 GROUP BY,因为我想包含没有响应的功能。
这是我正在尝试的 SQL,但它在 TIMESTAMPDIFF 子句附近报告 SQL 语法错误。
SELECT f.id, f.name, f.description, is_active,
(SELECT COUNT(r.id) FROM responses r
LEFT JOIN participants p ON r.id_participant = p.id
WHERE p.is_ignored IS NULL
AND r.is_deleted IS NULL
AND r.id_feature = f.id
AND r.result IS NULL) AS nr_s,
(SELECT COUNT(r.id) FROM responses r
LEFT JOIN participants p ON r.id_participant = p.id
WHERE p.is_ignored IS NULL
AND r.is_deleted IS NULL
AND r.id_feature = f.id) AS nr_r,
(SELECT SUM(r.is_D) FROM responses r
LEFT JOIN participants p ON r.id_participant = p.id
WHERE p.is_ignored IS NULL
AND r.is_deleted IS NULL
AND r.id_feature = f.id) AS is_D,
(SELECT AVG(TIMESTAMPDIFF(SECONDS,r.began,r.ended)) FROM responses r
LEFT JOIN participants p ON r.id_participant = p.id
WHERE p.is_ignored IS NULL
AND r.is_deleted IS NULL
AND r.id_feature = f.id) AS duration
FROM features f
WHERE f.is_deleted IS NULL AND f.id_survey=44
ORDER BY f.id ASC
我究竟做错了什么?