1

所以伙计们,我正在尝试实现嵌套集模型,我有以下查询

SELECT 
    n.CategoryID, 
    CategoriesI18n.CategoryName, 
    COUNT(*)-1 AS level FROM Categories AS n, 
    Categories AS p LEFT JOIN CategoriesI18n ON (
        n.CategoryID=CategoriesI18n.CategoryID AND CategoriesI18n.Locale="en-US"
    ) WHERE n.lft BETWEEN p.lft AND p.rgt GROUP BY n.lft ORDER BY n.lft;

当我选择树时,我必须加入 CategoriesI18n 表,这是当前类别的多语言行。但我得到这个错误

Unknown column 'n.CategoryID' in 'on clause'

我不知道为什么。也许 Categories AS p 会覆盖第一个别名?

4

2 回答 2

2

没关系,我想通了。不得不做嵌套选择。

SELECT n.CategoryID, n.CategoryName, COUNT(*)-1 AS level 
    FROM (
        SELECT Categories.CategoryID AS CategoryID,  Categories.lft AS lft,  Categories.rgt AS rgt, CategoriesI18n.CategoryName AS CategoryName FROM Categories LEFT JOIN CategoriesI18n ON (Categories.CategoryID=CategoriesI18n.CategoryID AND CategoriesI18n.Locale="bg-BG")
        ) AS n, Categories AS p 
    WHERE n.lft BETWEEN p.lft AND p.rgt GROUP BY n.lft ORDER BY n.lft;
于 2013-11-10T10:47:42.347 回答
0

'Categories AS p LEFT JOIN CategoriesI18n',the joined Categories's alias is 'p',not 'n'

于 2013-11-10T09:54:24.527 回答