DATEDIFF 如果值不为空
DATEDIFF if a value IS NOT NULL
这是我之前发布的 的后续内容。
我正在使用以下语法计算 DATEDIFF 的 SUM
SUM(DATEDIFF(COALESCE(e.time_period_to, NOW()), e.time_period_from)) / COUNT(DISTINCT e.company_id)
我现在想要的是仅当 e.time_period_from is NOT NULL
时计算 DATEDIFF
的 SUM
我尝试了以下查询。
SUM(IF(e.time_period_from IS NOT NULL), DATEDIFF(COALESCE(e.time_period_to, NOW()), e.time_period_from), 0) / COUNT(DISTINCT e.company_id)
这给我 SQL 语法错误。
怎么办?
更新:
这是我的完整 MySQL 查询
SELECT
SQL_CALC_FOUND_ROWS
u.id,
u.name,
u.email,
COUNT(DISTINCT(question_id)) as number_of_answered_questions,
(SELECT option_id FROM answer WHERE user_id = u.id GROUP BY option_id ORDER BY COUNT(option_id) DESC LIMIT 1) as option_id,
SUM(IF(e.time_period_from IS NOT NULL), DATEDIFF(COALESCE(e.time_period_to, NOW()), e.time_period_from), 0) / COUNT(DISTINCT e.company_id) AS tenure_in_days
FROM
user u
LEFT JOIN
role r ON (r.id = u.role_id)
LEFT JOIN
answer a ON (a.user_id = u.id)
LEFT JOIN
employment e ON (e.user_id = u.id)
WHERE
r.slug = 'app_user'
GROUP BY
u.id
LIMIT
0, 10
如您所见,这是针对子 select 我不能在其外部放置 where 条件。
这是我得到的错误。
"SQLSTATE[42000]: Syntax error or access violation: 1064 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 '), DATEDIFF(COALESCE(e.time_period_to, NOW()), e.time_period_from), 0) / COUNT(D' at line 1"
谢谢。
就像我之前的评论一样,我认为你只是把括号设置错了你想要得到的是这样的:
SUM(IF(e.time_period_from IS NOT NULL, DATEDIFF(COALESCE(e.time_period_to, NOW()), e.time_period_from), 0))
/ COUNT(DISTINCT e.company_id)
我检查你的查询
此行错误
SUM(IF(e.time_period_from IS NOT NULL), DATEDIFF(COALESCE(e.time_period_to, NOW()), e.time_period_from), 0)
在行代码
上方使用此代替
SUM(IF(e.time_period_from IS NOT NULL, DATEDIFF(COALESCE(e.time_period_to, NOW()), e.time_period_from), 0));
这是我之前发布的
我正在使用以下语法计算 DATEDIFF 的 SUM
SUM(DATEDIFF(COALESCE(e.time_period_to, NOW()), e.time_period_from)) / COUNT(DISTINCT e.company_id)
我现在想要的是仅当 e.time_period_from is NOT NULL
DATEDIFF
的 SUM
我尝试了以下查询。
SUM(IF(e.time_period_from IS NOT NULL), DATEDIFF(COALESCE(e.time_period_to, NOW()), e.time_period_from), 0) / COUNT(DISTINCT e.company_id)
这给我 SQL 语法错误。
怎么办?
更新:
这是我的完整 MySQL 查询
SELECT
SQL_CALC_FOUND_ROWS
u.id,
u.name,
u.email,
COUNT(DISTINCT(question_id)) as number_of_answered_questions,
(SELECT option_id FROM answer WHERE user_id = u.id GROUP BY option_id ORDER BY COUNT(option_id) DESC LIMIT 1) as option_id,
SUM(IF(e.time_period_from IS NOT NULL), DATEDIFF(COALESCE(e.time_period_to, NOW()), e.time_period_from), 0) / COUNT(DISTINCT e.company_id) AS tenure_in_days
FROM
user u
LEFT JOIN
role r ON (r.id = u.role_id)
LEFT JOIN
answer a ON (a.user_id = u.id)
LEFT JOIN
employment e ON (e.user_id = u.id)
WHERE
r.slug = 'app_user'
GROUP BY
u.id
LIMIT
0, 10
如您所见,这是针对子 select 我不能在其外部放置 where 条件。
这是我得到的错误。
"SQLSTATE[42000]: Syntax error or access violation: 1064 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 '), DATEDIFF(COALESCE(e.time_period_to, NOW()), e.time_period_from), 0) / COUNT(D' at line 1"
谢谢。
就像我之前的评论一样,我认为你只是把括号设置错了你想要得到的是这样的:
SUM(IF(e.time_period_from IS NOT NULL, DATEDIFF(COALESCE(e.time_period_to, NOW()), e.time_period_from), 0))
/ COUNT(DISTINCT e.company_id)
我检查你的查询 此行错误
SUM(IF(e.time_period_from IS NOT NULL), DATEDIFF(COALESCE(e.time_period_to, NOW()), e.time_period_from), 0)
在行代码
上方使用此代替 SUM(IF(e.time_period_from IS NOT NULL, DATEDIFF(COALESCE(e.time_period_to, NOW()), e.time_period_from), 0));