1. 程式人生 > 其它 >Every derived table must have its own alias

Every derived table must have its own alias

技術標籤:資料庫問題彙總MySQL

背景:在進行聯表查詢時,出現了Every derived table must have its own alias的錯誤。

錯誤程式碼:

        SELECT t1.id FROM zhfw_service_reply t1
        JOIN(
        SELECT id FROM zhfw_service_theme t3  WHERE
        updated_time < #{time}
        AND section_code = 'service'
        ) on t3.id = t1.theme_id

原因:一時疏忽,漏了表的別名。查詢產生新的表每一個派生出來的表都必須有一個自己的別名。即JOIN的結果需要一個新的名字。改為如下:

        SELECT t1.id FROM zhfw_service_reply t1
        JOIN(
        SELECT id FROM zhfw_service_theme  WHERE
        updated_time < #{time}
        AND section_code = 'service'
        )t3 on t3.id = t1.theme_id

注意t3的位置。