这是一个我无法解释的奇怪现象:
FROM (
SELECT @report_date :=
IF (DAYOFWEEK(CURDATE()) BETWEEN 3 AND 7, DATE_SUB(CURDATE(), INTERVAL 1 DAY),
IF (DAYOFWEEK(CURDATE()) = 1, DATE_SUB(CURDATE(), INTERVAL 2 DAY),
IF (DAYOFWEEK(CURDATE()) = 2, DATE_SUB(CURDATE(), INTERVAL 3 DAY), NULL)))
) AS dt
, t1
LEFT JOIN t2 ON t1.id = t2.id
http://sqlfiddle.com/#!2/9e0c4/3
作品。
FROM t1
, (
SELECT @report_date :=
IF (DAYOFWEEK(CURDATE()) BETWEEN 3 AND 7, DATE_SUB(CURDATE(), INTERVAL 1 DAY),
IF (DAYOFWEEK(CURDATE()) = 1, DATE_SUB(CURDATE(), INTERVAL 2 DAY),
IF (DAYOFWEEK(CURDATE()) = 2, DATE_SUB(CURDATE(), INTERVAL 3 DAY), NULL)))
) AS dt
LEFT JOIN t2 ON t1.id = t2.id
http://sqlfiddle.com/#!2/9e0c4/2
不起作用,错误:
Unknown column 't1.id' in 'on clause': ...
为什么?注意子句t1
中声明where 的顺序。FROM
请注意,我不一定要寻找替代方案,我只是想了解为什么这里的订单会导致 a 出现错误JOIN
,我认为在该FROM
子句完成后会发生这种错误。