语法错误或访问冲突:1066不是唯一的表/别名:'任务'

时间:2018-09-28 22:38:30

标签: php mysql

我一直收到此错误,并只找到laravel用户的答案。

我有这个查询:

  $stmt = $pdo->query("SELECT branches.branch_id, branches.branch_name,
        tasks.task_name, tasks.task_status, tasks.real_amount,
        tasks.task_start, tasks.task_finish, tasks.description, projects.proj_name, projects.project_id
        FROM retail2.branches, retail2.projects, retail2.tasks
        JOIN tasks ON tasks.branch_id = branches.branch_id;
        JOIN branches_projects ON projects.project_id = branches_projects.proj_id
        JOIN branches ON branches.branch_id = branches_projects.branch_id");

并且我一直在尝试调试,但是我找不到问题所在……有什么建议吗?谢谢!

1 个答案:

答案 0 :(得分:1)

您无需同时在FROMJOIN子句中列出表。您只需要对JOIN子句重新排序,因为JOIN只能引用先前JOINFROM子句中的表。

在查询中间,;行的结尾处还有一个JOIN tasks。之后的所有内容都不是查询的一部分,这将导致错误,因为PDO不允许在一个调用中进行多个查询。

SELECT branches.branch_id, branches.branch_name,
    tasks.task_name, tasks.task_status, tasks.real_amount,
    tasks.task_start, tasks.task_finish, tasks.description, projects.proj_name, projects.project_id
    FROM projects
    JOIN branches_projects ON projects.project_id = branches_projects.proj_id
    JOIN branches ON branches.branch_id = branches_projects.branch_id
    JOIN tasks ON tasks.branch_id = branches.branch_id;