如何在WHERE之后加入JOIN?

时间:2019-04-26 06:06:33

标签: sql oracle join

我需要执行一个遵循给定前缀的语句。

我们使用某个程序,它具有“搜索文件夹”。我可以在Search文件夹中定义一个SQL查询。该文件夹具有查询的内置部分,我无法对其进行修改。

内置部分是:

Prefix Artikelfilter: SELECT * FROM CMKAT.ART WHERE 1=1 AND 

现在,我想执行以下搜索查询:

select * from CMKAT.ART

join CMKAT.AEZ on art.artKEYI = aez.aezartkeyi

join pro on pro.prokeyi = aez.aezprokeyi

left outer join psz on pro.prokeyi = psz.pszprokeyi

WHERE psz.pszprokeyi is null

AND pro.proetykeyi = 1;

当然,“ SELECT * ...”部分是多余的,但是如何使查询起作用? JOIN在WHERE之后。

我知道开发人员通过创建视图并从视图中进行选择(而不是直接在查询中)解决了此问题。

编辑:

异常:JdbcAccess.execute失败(ORA-00904:“ KVTBEZC”:ungültigeID )。

 SQL: 
<prefix>
SELECT /* filter art */ artkeyi, artkavkeyi, artvnrs FROM art  JOIN 
kav ON art.artkavkeyi = kav.kavkeyi  JOIN kvt ON kav.kavkvtkeyi = 
kvt.kvtkeyi  JOIN kat ON kav.kavkatkeyi = kat.katkeyi  WHERE 
(artkavkeyi IN (587, 3075, 7, 8, 592)) AND

</prefix>

 1 = 0 union all select * 
from cmkat.art  join cmkat.aez on art.artkeyi = aez.aezartkeyi  join 
pro on pro.prokeyi = aez.aezprokeyi  left join psz on pro.prokeyi = 
psz.pszprokeyi  where psz.pszprokeyi is null and pro.proetykeyi = 1 
AND katkeyi = 4 AND (katspes IN (0, 3, 1, 2))/* join-dummy */ ORDER BY 
artkeyi, kvtbezc (DATABASE_EXECUTE_FAILED)

3 个答案:

答案 0 :(得分:1)

我将以这种方式构造SQL:

SELECT * FROM CMKAT.ART WHERE 1=1 AND EXISTS (select 1 from CMKAT.AEZ, pro left outer join psz on pro.prokeyi = psz.pszprokeyi where aez.aezartkeyi = art.artKEYI and pro.prokeyi = aez.aezprokeyi and psz.pszprokeyi is null AND pro.proetykeyi = 1);

答案 1 :(得分:1)

我想您需要像这样的正确查询:

 If exist (SELECT * FROM CMKAT.ART WHERE 1=1)
 Begin
 (
 select  * 
 from CMKAT.ART 
 join CMKAT.AEZ on art.artKEYI = aez.aezartkeyi
 join pro on pro.prokeyi = aez.aezprokeyi 
 left outer join psz on pro.prokeyi = psz.pszprokeyi 
 WHERE psz.pszprokeyi is null AND pro.proetykeyi = 1;
 End
 )     

答案 2 :(得分:0)

我不知道您的工具是否允许这种构造,但是您可以使用union all作弊:

select * from cmkat.art where 1 = 1 and 1 = 0
union all
select cmkat.art.* 
  from cmkat.art
  join cmkat.aez on art.artkeyi = aez.aezartkeyi
  join pro on pro.prokeyi = aez.aezprokeyi
  left join psz on pro.prokeyi = psz.pszprokeyi
  where psz.pszprokeyi is null and pro.proetykeyi = 1;

第一行由于1 = 0条件而删除了原始查询的数据,因此我们仅从联合的第二部分获取数据。

很遗憾,您只能获得art表的列,因为您的工具不允许更改所选数据。但是,您可以从art列的代替表中选择一些列来更改它。