我有3张桌子
1项目
2项目ID,活动编号
3活动
我需要在这些表格中进行搜索,我试图使内部联接继续进行
1066 - 不唯一的表/别名:'pp086_projects'
我正在寻找“Next Nine”,这是在竞选活动中。
我的SQL查询是:
SELECT * FROM `PP086_projects`, `PP086_project_users`,pp086_project_companies
JOIN `PP086_projects` ON `PP086_projects`.id = pp086_project_companies.project_id
JOIN pp086_project_companies ON pp086_companies.id==pp086_project_companies.company_id
WHERE (`PP086_projects`.`id` = `PP086_project_users`.`project_id`
AND `PP086_project_users`.`user_id` = '2')
AND ( name LIKE '%Next Nine%'
OR name LIKE 'Next Nine'
OR description LIKE 'Next Nine'
OR pp086_project_companies.name LIKE 'Next Nine' )
答案 0 :(得分:1)
您正在混合隐式和显式join
语法。因此,您已将表PP086_projects
放入from
子句中两次。这可能是导致问题的原因。
作为一个简单的规则,只是不要在,
子句中使用from
。如果您想要交叉联接,请明确使用cross join
:
SELECT *
FROM `PP086_projects` join
`PP086_project_users`
on `PP086_projects`.`id` = `PP086_project_users`.`project_id`
pp086_project_companies
ON `PP086_projects`.id = pp086_project_companies.project_id JOIN
pp086_project_companies
ON pp086_companies.id = pp086_project_companies.company_id
WHERE (`PP086_project_users`.`user_id` = '2') AND
( name LIKE '%Next Nine%'
OR name LIKE 'Next Nine'
OR description LIKE 'Next Nine'
OR pp086_project_companies.name LIKE 'Next Nine'
);
答案 1 :(得分:0)
您可以多次使用PP086_projects
表。您应该为它的不同实例添加别名。
这是不明确的,例如:PP086_projects.id
或者,如果这是无意的,可能你应该在FROM之后删除表的名称,如下所示:
SELECT * FROM `PP086_projects`
JOIN `PP086_project_users` ON `PP086_projects`.`id` = `PP086_project_users`.`project_id`
JOIN `pp086_project_companies` ON `PP086_projects`.id = pp086_project_companies.project_id
JOIN pp086_companies ON pp086_companies.id== pp086_project_companies.company_id
WHERE ( AND `PP086_project_users`.`user_id` = '2') AND ( name LIKE '%Next Nine%'
OR name LIKE 'Next Nine'
OR description LIKE 'Next Nine'
OR pp086_project_companies.name LIKE 'Next Nine' )