我遇到了- Using ALIAS column in WHERE最近,我了解了WHERE子句为什么会给我一个错误,但是我似乎无法找出其他任何方式来编写嵌套查询。 SELECT子查询本身可以完全正常运行。
错误如下:
Error Code: 1054. Unknown column 'actual_start_time' in 'IN/ALL/ANY subquery'
表结构:
学生:student_id student_name登录时间logout_time
测试:test_id test_start_time test_end_time
TestStats:test_id student_id test_duration
UPDATE test_stats
SET test_duration = datediff(hour, actual_start_time - actual_end_time)
WHERE (actual_start_time, actual_end_time)
IN (
SELECT
CASE
WHEN (s.login_time > t.test_start_time) THEN s.login_time
ELSE t.test_start_time
END AS actual_start_time,
CASE
WHEN (s.logout_time < t.test_end_time) THEN s.logout_time
ELSE t.test_end_time
END AS actual_end_time
FROM tests AS t, students AS s, test_stats AS ts
WHERE t.test_id = ts.test_id and s.student_id = ts.student_id);
答案 0 :(得分:1)
这似乎是您想要的:
UPDATE test_stats ts JOIN
tests t
ON t.test_id = ts.test_id JOIN
students s
ON s.student_id = ts.student_id
SET test_duration = timestampdiff(hour,
GREATEST(s.login_time, t.test_start_time),
LEAST(s.logout_time, t.test_end_time)
);