【问题标题】:oracle sql request column ambiguously definedoracle sql请求列定义不明确
【发布时间】:2017-12-07 17:30:02
【问题描述】:

我正在使用 oracle 11g 并尝试执行此请求

select code_mod,INTITULE,code_et,nom ,avg(note)
from note,exam,module,etudiant
where note.CODE_EX = exam.CODE_EX 
and EXAM.CODE_MOD=MODULE.CODE_MOD
and NOTE.CODE_ET = ETUDIANT.CODE_ET
group by code_mod,code_et 
order by code_mod;

但它说!

 ORA-00918: column ambiguously defined
00918. 00000 -  "column ambiguously defined"
*Cause:    
*Action:
Error on line 6, colunn 19

这有什么问题?如果我执行这个请求,它会起作用

select *
from note,exam,module,etudiant
where note.CODE_EX = exam.CODE_EX 
and EXAM.CODE_MOD=MODULE.CODE_MOD
and NOTE.CODE_ET = ETUDIANT.CODE_ET;

【问题讨论】:

标签: sql oracle oracle11g ora-00918


【解决方案1】:

note,exam,module,etudiant 表中至少有两个 code_mod,INTITULE,code_et,nom 列,并且不使用别名。

例如moduleexam 表都包含code_mod 列,并且在选择列表中您没有显示它的来源

这样使用:

select m.code_mod,intitule,et.code_et,nom ,avg(note)
  from note n
 inner join exam e on ( n.code_ex = e.code_ex )
 inner join module m on ( e.code_mod=m.code_mod )
 inner join etudiant et on ( et.code_et = n.code_et )
group by m.code_mod,intitule,et.code_et,nom 
order by m.code_mod;

并且你应该在group by 表达式中包含所有不带grouping functions 的列。

【讨论】:

  • 之前已经尝试过了,但是 ORA-00979: not a GROUP BY expression 00979. 00000 - "not a GROUP BY expression"
【解决方案2】:

您在查询涉及的多个表中具有相同名称的列,因此您必须在列前加上正确的表名,例如:

select 
    EXAM.code_mod, INTITULE, EXAM.code_et, nom, avg(note)
from 
    note, exam, module, etudiant
where 
    note.CODE_EX = exam.CODE_EX 
    and EXAM.CODE_MOD=MODULE.CODE_MOD
    and NOTE.CODE_ET = ETUDIANT.CODE_ET
group by 
    EXAMcode_mod, EXAM.code_et, INTITULE, nom
order by 
    EXAM.code_mod;

【讨论】:

  • 我在提问之前试过了,但它说 Erreur SQL : ORA-00979: not a GROUP BY expression 00979. 00000 - "not a GROUP BY expression"
  • 用group by中所有不在聚合函数中的列更新答案
猜你喜欢
  • 2011-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-02
  • 2021-09-19
  • 1970-01-01
相关资源
最近更新 更多