0

我有两个表featuresresponses,其中每个功能可能有多个响应(或者可能是零响应)。响应表有一个包含时间戳的beganandended列(至少会有一个开始时间戳,结束可能为 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

我究竟做错了什么?

4

1 回答 1

0

SECONDS不是有效单位。你想要SECOND

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(SECOND,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
于 2013-10-30T09:37:07.790 回答