Ambiguous column query

帅比萌擦擦* 提交于 2019-12-13 04:17:17

问题


I'm getting the "ambiguous column" error when running this query, but I'm having a hard time finding the cause:

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||'%');

Which columns am I messing up?

Thank you so much for your time!


回答1:


You can NOT reference column aliases in the SELECT clause within the WHERE clause, at least not in the same query. You could subquery it, or just use the original column references.

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||'%');



回答2:


In your WHERE clause, you need to reference the columns in the table not the aliases you assign in the SELECT list. So you cannot refer to the aliases language, secondary_language, or genre. You would need to refer to the actual columns in the actual table. I'll wager that you want something like

or
bolanguages.LANGUAGE = :P19_LANGUAGE
or
bolanguages2.language = :P19_LANGUAGE)
AND
(:P19_GENRE = 'all' 
or
bogenres.NAME = :P19_GENRE)



回答3:


The question really isn't answerable without knowing your table structure. There are a few columns listed with no table alias (I see language, secondary_language, and genre), make sure every column has a table alias and the problem will most likely go away.

Actually, taking a second look, I'm 99% sure it is language causing the problem: you have an alias in your select, two copies of the BO_LANGUAGES table, and a BO_BOOKS table that all have a field with that name. Change it to bolanguages.language in the language part of the WHERE clause (personally, I'd change secondary_language and genre also).



来源:https://stackoverflow.com/questions/13754479/ambiguous-column-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!