别名和带有名称的选择列表中的字段之间的不明确字段名称
Posted
技术标签:
【中文标题】别名和带有名称的选择列表中的字段之间的不明确字段名称【英文标题】:Ambiguous field name between an alias and a field in the select list with name 【发布时间】:2018-09-08 14:27:51 【问题描述】:我有一个 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_clas-s-room, number_of_clas-s-room, specific,
clas-s-rooms.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 clas-s-rooms using(id_clas-s-room) join departments using(id_department))
join groups using (id_group) join departments using(id_department))
这是一个初始化计划项目的查询,每个项目都属于自己的部门,所以我每次都需要添加部门表。 数据库架构:Database schema
我附上数据库文件example 当前版本的火鸟:2.5.7
【问题讨论】:
您可能需要在引用该字段的所有其他位置指定您正在使用哪个表的id_department
字段(就像您在顶部使用 teachersanddepartments.id_department
一样)。
能否将您的查询减少到重现此问题所需的最低限度,并提供必要的 DDL。检查minimal reproducible example 以获取信息。我猜这是 Firebird 不知道选择哪个表来连接的结果。您可能需要明确指定 on <table1>.id_department = <table2>.id_department
而不仅仅是 using(id_department)
我测试了这个理论,并通过一个简单的查询似乎并非如此,所以有一个简单的可重现示例进行实验会非常有帮助。
我编辑了我的问题,添加了一些信息。我尝试提出建议的选项,它不起作用,错误仍然存在。
【参考方案1】:
问题是您重复加入表departments
而不给它起别名。然后,当您在选择列表中引用 departments.id_department
时,Firebird 不知道它需要使用四个 departments
实例中的哪一个。
您要么需要更改查询,以便只引用一次departments
,要么如果出于某种原因需要它(我认为您不需要,如 scaisEdge 的回答所示),您需要显式别名每次出现departments
,然后用正确的别名限定列名。
【讨论】:
啊,这不是问题,因为departments.id_department
甚至不在选择列表中。我先把它留在这里,直到我能准确找出哪里出了问题,因为我怀疑它会暗示实际问题出在哪里。【参考方案2】:
您有几个不明确的列,第一个出现在 departments
、teachersanddepartments
和 teachers
的嵌套连接中。
但是每次您为不同的表使用相同的列名时。你有同样的问题,所以我已经消除了teachersanddepartments
、subjects
、clas-s-rooms
、groups
的列:
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_clas-s-room
, number_of_clas-s-room
, clas-s-rooms.specific
, clas-s-rooms.id_department
, clas-s-rooms.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 clas-s-rooms using(id_clas-s-room) 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_clas-s-room
, number_of_clas-s-room
, clas-s-rooms.specific
, clas-s-rooms.id_department
, clas-s-rooms.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 clas-s-rooms using(id_clas-s-room)
join groups using (id_group)
正如 Mark Rotteveel 在正确答案中所建议的,这可能是您错误的原因
【讨论】:
我不完全确定这是问题所在,因为我尝试了具有相同列名的表之间的多个连接,而 Firebird 没有给出此错误。 我已经查看了查询,并且在涉及教师、教师和部门以及部门的连接的嵌套部分中,使用 using(department_id) 指的是 ambiguos 列。因为部门 ID 存在于所有 3 个表中 当然可以,但是我已经尝试在具有同一列的表之间使用using
进行多次连接,而 Firebird 并没有给我这个错误。该错误专门针对选择列表,这让我想知道问题中显示的错误是否可能实际上不是指不同的列(因为选择列表中的所有 id_department 列都是合格的)。
无论如何,department_id 的歧义只是 where 子句解析中第一个遇到的问题.. 在查询中还有其他歧义.. 我认为 OP 可以为我们提供一些信息.. 对于歧义情况..
我认为您的最新编辑显示了实际问题。看起来确实是同一张表的重复连接。我在代码的密度上错过了这一点。【参考方案3】:
我做到了,我为所有部门表添加了别名。谢谢大家!你帮帮我=)
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_clas-s-room, number_of_clas-s-room, clas-s-rooms.specific, clas-s-rooms.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 clas-s-rooms using(id_clas-s-room) join departments d3 on d3.id_department = clas-s-rooms.id_department)
join groups using (id_group) join departments d4 on d4.id_department = groups.id_department)
【讨论】:
以上是关于别名和带有名称的选择列表中的字段之间的不明确字段名称的主要内容,如果未能解决你的问题,请参考以下文章
django.db.utils.OperationalError:(1052,“字段列表中的列'名称'不明确”)