這裏後隱含聲明是一個怪胎,我無法解釋:FROM子句原因設置可變JOIN錯誤,如果表
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': ...
爲什麼?請注意在FROM
條款中聲明t1
的順序。
注意,我沒有必要尋找替代品,我只是想知道爲什麼這裏的秩序造成一個JOIN
到錯誤時FROM
條款完成,我會想到下面會發生。
如果有人對我如何在這裏結束感興趣,請參閱this broken fiddle和this working fiddle以獲得真實查詢的基本示例。
請注意,一個非常類似的問題:http://stackoverflow.com/a/761599/451969 –