不明确的列查询

时间:2012-12-06 23:25:47

标签: sql oracle plsql multiple-columns ambiguous

运行此查询时出现“模糊列”错误,但我很难找到原因:

select bobooks.ID request,    
       bobooks.TITLE,    
       bobooks.AUTHOR,    
       bogenres.NAME genre,    
       bobooks.OWNER,    
       bostatus.NAME status,    
       bolanguages.LANGUAGE language,    
       bolanguages2.LANGUAGE secondary_language    
from BO_BOOKS bobooks    
inner join BO_GENRES bogenres
  on bobooks.genre = bogenres.id    
inner join BO_STATUS bostatus
  on bobooks.status = bostatus.id    
inner join BO_LANGUAGES bolanguages
  on bobooks.language = bolanguages.id    
left outer join BO_LANGUAGES bolanguages2
  on bobooks.secondary_language = bolanguages2.id    
where (replace(:P19_AUTHOR, ' ', '') = '' or
       bobooks.author like '%'||:P19_AUTHOR||'%') AND    
      (replace(:P19_TITLE, ' ', '') = '' or
       bobooks.title like '%'||:P19_TITLE||'%') AND    
      (:P14_LANGUAGE = 'all' or
       language = :P19_LANGUAGE or
       secondary_language = :P19_LANGUAGE) AND
      (:P19_GENRE = 'all' or
       genre = :P19_GENRE) AND
      (replace(:P19_OWNER, ' ', '') = ''  or
       bobooks.owner like '%'||:P19_OWNER||'%');

我搞哪些栏目?

非常感谢您的时间!

3 个答案:

答案 0 :(得分:4)

您不能在WHERE子句中的SELECT子句中引用列别名,至少不能在同一查询中引用。您可以对其进行子查询,或者只使用原始列引用。

select     
 bobooks."ID" request,    
 bobooks."TITLE",    
 bobooks."AUTHOR",    
 bogenres."NAME" genre,    
 bobooks."OWNER",    
 bostatus."NAME" status,    
 bolanguages."LANGUAGE" language,    
 bolanguages2."LANGUAGE" secondary_language    
from BO_BOOKS bobooks    
inner join    
BO_GENRES bogenres on bobooks.genre = bogenres.id    
inner join     
BO_STATUS bostatus on bobooks.status = bostatus.id    
inner join     
BO_LANGUAGES bolanguages on bobooks.language = bolanguages.id    
left outer join    
BO_LANGUAGES bolanguages2 on bobooks.secondary_language = bolanguages2.id    
where     
(replace(:P19_AUTHOR, ' ', '') = '' 
or
bobooks.author like '%'||:P19_AUTHOR||'%')
AND    
(replace(:P19_TITLE, ' ', '') = '' 
or
bobooks.title like '%'||:P19_TITLE||'%')
AND    
(:P14_LANGUAGE = 'all' 
or
bolanguages."LANGUAGE" = :P19_LANGUAGE
or
bolanguages2."LANGUAGE" = :P19_LANGUAGE)
AND
(:P19_GENRE = 'all' 
or
bogenres."NAME" = :P19_GENRE)
AND
(replace(:P19_OWNER, ' ', '') = '' 
or
bobooks.owner like '%'||:P19_OWNER||'%');

答案 1 :(得分:0)

WHERE子句中,您需要引用表中的列,而不是您在SELECT列表中指定的别名。因此,您无法引用别名languagesecondary_languagegenre。您需要参考实际表中的实际列。我打赌你想要像

这样的东西
or
bolanguages.LANGUAGE = :P19_LANGUAGE
or
bolanguages2.language = :P19_LANGUAGE)
AND
(:P19_GENRE = 'all' 
or
bogenres.NAME = :P19_GENRE)

答案 2 :(得分:0)

如果不知道你的表结构,这个问题真的无法回答。列出了几个没有表别名的列(我看到languagesecondary_languagegenre),请确保每个列都有一个表别名,问题很可能会消失。

实际上,再看看,我99%肯定它是language导致问题:你的select中有一个别名,BO_LANGUAGES表的两个副本和一个BO_BOOKS表都有一个具有该名称的字段。在WHERE子句的语言部分将其更改为bolanguages.language(就个人而言,我也会更改secondary_languagegenre)。

相关问题