2014-09-24 84 views
0

我在MySQL 5.5.3下執行這個查詢時遇到了一些問題。它返回查詢錯誤(1066):不是唯一的表/別名:「類」查詢錯誤(1066) - MySQL

SELECT COUNT(DISTINCT lra.userid) AS learners, COUNT(DISTINCT tra.userid) AS teachers 
FROM mdl_course AS courses, mdl_course_categories AS categories 
LEFT JOIN mdl_context AS ctx ON c.id = ctx.instanceid 
JOIN mdl_role_assignments AS lra ON lra.contextid = ctx.id 
JOIN mdl_role_assignments AS tra ON tra.contextid = ctx.id 
JOIN mdl_course_categories AS categories ON courses.category = categories.id 
WHERE courses.category = categories.id 
AND (
    categories.path LIKE '%/9/%' 
    OR categories.path LIKE '%/9' 
    ) 
AND lra.roleid=5 
AND tra.roleid=3 

我在一個小的損失的是我已經使用了別名列以及表名。任何人都可以發現我可能出錯的地方嗎?

回答

1

你有一個SELECT語句作爲

FROM mdl_course AS courses, mdl_course_categories AS categories 

然後

JOIN mdl_course_categories AS categories ON courses.category = categories.id 

這使得非唯一的別名

您應該刪除選擇部分,應該作爲

SELECT COUNT(DISTINCT lra.userid) AS learners, COUNT(DISTINCT tra.userid) AS teachers 
FROM mdl_course AS courses 
LEFT JOIN mdl_context AS ctx ON c.id = ctx.instanceid 
JOIN mdl_role_assignments AS lra ON lra.contextid = ctx.id 
JOIN mdl_role_assignments AS tra ON tra.contextid = ctx.id 
JOIN mdl_course_categories AS categories ON courses.category = categories.id 
+0

十分感謝!我知道這對我來說會很愚蠢!只要我被允許,我會盡快接受。 – 2014-09-24 11:03:20

+0

非常歡迎。 – 2014-09-24 11:06:57

0

在第二行查詢中,使用表名,並且在第6行中,您再次使用了表。 如

FROM mdl_course AS courses, mdl_course_categories AS categories 

而且

JOIN mdl_course_categories AS categories ON courses.category = categories.id