别名和选择列表中具有名称的字段之间的字段名称不明确

时间:2018-09-08 14:27:51

标签: sql firebird firebird2.5

我有一个SQL查询使用单词“ using”链接许多表,并且其中一个表出现在每个元素中,错误:

  

别名和选择列表中具有该名称的字段之间的字段名称不明确。 ID_DEPARTMENT

如何正确更改查询?请求链接:https://pastebin.com/t2rqMAut

select id_teacher, fio, post, teachersanddepartments.id_department, 
       name_of_department, id_subject, name_of_subject, specific, 
       subjects.id_department, name_of_department,
       id_classroom, number_of_classroom, specific, 
       classrooms.id_department, name_of_department, id_group, 
       name_of_group, groups.id_department, name_of_department,
       specifics, daytime, pair, NUMERATOR_DENOMINATOR, keyy, typekey
from ((((classes join teachers using(id_teacher) join teachersanddepartments 

using(id_teacher) join departments using(id_department))
join subjects using (id_subject) join departments using(id_department))
join classrooms using(id_classroom) join departments using(id_department))
join groups using (id_group) join departments using(id_department))

这是一个初始化计划项目的查询,每个项目都属于自己的部门,因此我每次都需要添加部门表。 数据库架构:Database schema

我附加了数据库文件example 当前版本的Firebird:2.5.7

3 个答案:

答案 0 :(得分:1)

问题是您反复加入表departments而没有别名。当您在选择列表中引用departments.id_department时,Firebird不知道它需要使用departments的四个实例中的哪个。

您需要更改查询,以便仅引用一次departments,或者由于某种原因(如scaisEdge的答案所示,我不认为您需要这样做),则需要显式地为每次出现的departments加上别名,然后使用正确的别名限定列名。

答案 1 :(得分:1)

您有几个歧义的列,如果是departmentsteachersanddepartmentsteachers的嵌套联接,则第一个出现。

但是每次您对不同的表使用相同的列名时。您也遇到同样的问题,因此我为teachersanddepartmentssubjectsclassroomsgroups的列进行了歧义消除:

select id_teacher
      , fio
      , post
      , teachersanddepartments.id_department
      , teachersanddepartments.name_of_department
      , id_subject
      , name_of_subject
      , specific
      , subjects.id_department
      , subjects.name_of_department
      , id_classroom
      , number_of_classroom
      , classrooms.specific
      , classrooms.id_department
      , classrooms.name_of_department
      , id_group
      , name_of_group
      , groups.id_department
      , groups.name_of_department
      , groups.specifics
      , daytime
      , pair
      , NUMERATOR_DENOMINATOR
      , keyy
      , typekey
from (
      (
        (
          ( classes join teachers using(id_teacher) 
            join teachersanddepartments using(id_teacher) 
            join departments ON departments.id_department = teachersanddepartments.id_department
          )
            join subjects using (id_subject) join departments using(id_department)
        )
    join classrooms using(id_classroom) join departments using(id_department)
      )
    join groups using (id_group) join departments using(id_department)
) 

由于您正在使用INNER JOIN的事实,因此()对于JOIN似乎也是不必要和多余的。

使用嵌套联接,您将表departments联接3次;如果没有()嵌套联接,则只需要一个联接:

select id_teacher
      , fio
      , post
      , teachersanddepartments.id_department
      , teachersanddepartments.name_of_department
      , id_subject
      , name_of_subject
      , specific
      , subjects.id_department
      , subjects.name_of_department
      , id_classroom
      , number_of_classroom
      , classrooms.specific
      , classrooms.id_department
      , classrooms.name_of_department
      , id_group
      , name_of_group
      , groups.id_department
      , groups.name_of_department
      , groups.specifics
      , daytime
      , pair
      , NUMERATOR_DENOMINATOR
      , keyy
      , typekey
from classes join teachers using(id_teacher) 
join teachersanddepartments using(id_teacher) 
join departments ON departments.id_department = teachersanddepartments.id_department
join subjects using (id_subject) 

join classrooms using(id_classroom) 

join groups using (id_group) 

并且如Mark Rotteveel在正确答案中所建议,这可能是您出错的原因

答案 2 :(得分:0)

我做到了,我向所有部门表添加了别名。谢谢大家!你帮我=)

select id_teacher, fio, post, teachersanddepartments.id_department , d1.name_of_department, id_subject, name_of_subject, subjects.specific, subjects.id_department, d2.name_of_department,
    id_classroom, number_of_classroom, classrooms.specific, classrooms.id_department, d3.name_of_department, id_group, name_of_group, groups.id_department, d4.name_of_department,
    specifics, daytime, pair, NUMERATOR_DENOMINATOR, keyy, typekey
    from ((((classes join teachers using(id_teacher) join teachersanddepartments using(id_teacher) join departments d1 on d1.id_department = teachersanddepartments.id_department)
    join subjects using (id_subject) join departments d2 on d2.id_department = subjects.id_department)
    join classrooms using(id_classroom) join departments d3 on d3.id_department = classrooms.id_department)
    join groups using (id_group) join departments d4 on d4.id_department = groups.id_department)