0

运行此查询时出现“不明确的列”错误,但我很难找到原因:

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

我搞砸了哪些列?

非常感谢您的参与!

4

3 回答 3

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||'%');
于 2012-12-06T23:38:59.547 回答
0

在您的WHERE子句中,您需要引用表中的列而不是您在SELECT列表中分配的别名。因此,您不能引用别名languagesecondary_languagegenre。您需要参考实际表中的实际列。我敢打赌你想要类似的东西

or
bolanguages.LANGUAGE = :P19_LANGUAGE
or
bolanguages2.language = :P19_LANGUAGE)
AND
(:P19_GENRE = 'all' 
or
bogenres.NAME = :P19_GENRE)
于 2012-12-06T23:39:43.413 回答
0

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

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

于 2012-12-06T23:45:01.387 回答