简体   繁体   English

SQL Developer AND或语句

[英]SQL Developer AND OR Statements

Got a task at my job to get several fields from different tables into one sheet, using SQL Developer. 我的工作是使用SQL Developer将不同表中的多个字段合并为一张表。 Im a noob to SQL, however managed to build something. 我对SQL不熟悉,但是设法建立了一些东西。 Taking a look at my output learns me that the restrictions I built in do not work. 查看我的输出了解到我内置的限制不起作用。 Short description below. 简短说明如下。 Can someone please help me?! 有人可以帮帮我吗?! In my output I still see values other than 1006 in the ATINN field, values other tham Empty in the BWTAR field.. What am I doing wrong? 在我的输出中,我仍然在ATINN字段中看到1006以外的值,在BWTAR字段中看到thamEmpty的其他值。我在做什么错?

  1. 5 tables linked together 5张桌子连在一起

  2. Restriction on Users (DMSTRAATL, etc) 对用户的限制(DMSTRAATL等)

  3. Restriction on Productgroup (006*, etc) 对产品组的限制(006 *等)

  4. Restriction on some individual products 对某些个别产品的限制

  5. Restriction on product type (HERB, etc) 对产品类型的限制(草药等)

  6. Restriction on specific data field (All products should have atinn = 1006) 对特定数据字段的限制(所有产品的atinn = 1006)

  7. Restiction on specific data field (all products should have bwtar = empty) 对特定数据字段的限制(所有产品的bwtar =空)


SELECT 
    dmssap.mara.matnr, dmssap.mara.mtart, dmssap.mara.matkl, 
    dmssap.mara.ersda, dmssap.mara.ernam, dmssap.mara.bismt, 
    dmssap.marc.werks, dmssap.inob.cuobj, 
    LPAD(INOB.CUOBJ, 18, '0') AS CUOBJ_18, dmssap.ZMM_MATNR_MPO.matnr, dmssap.ZMM_MATNR_MPO.pname, 
    dmssap.ZMM_MATNR_MPO.stat, dmssap.mbew.bwkey, dmssap.mbew.bklas, 
    dmssap.mbew.bwtar, dmssap.mbew.vprsv, dmssap.mbew.bwtty, dmssap.mbew.verpr,
    dmssap.mbew.stprs, dmssap.ZMM_MATNR_MPO.matnr, dmssap.ZMM_MATNR_MPO.pname, 
    dmssap.ZMM_MATNR_MPO.stat, dmssap.ausp.atinn, dmssap.ausp.atwrt
FROM dmssap.mara
LEFT OUTER JOIN dmssap.marc
    ON (dmssap.marc.matnr) = (dmssap.mara.matnr) 
LEFT OUTER JOIN dmssap.ZMM_MATNR_MPO
    ON (dmssap.ZMM_MATNR_MPO.matnr) = (dmssap.mara.matnr)
LEFT OUTER JOIN dmssap.mbew
    ON CONCAT(dmssap.mbew.matnr, dmssap.mbew.bwkey) = CONCAT(dmssap.marc.matnr, dmssap.marc.werks)
LEFT OUTER JOIN dmssap.inob
    ON (dmssap.inob.objek) = (dmssap.mara.matnr)
LEFT OUTER JOIN dmssap.ausp
    ON dmssap.ausp.objek = LPAD(INOB.CUOBJ, 18, '0')
WHERE (dmssap.mara.ernam) IN (
    'DMSTRAATL', 'V0342628', 'V0343809', 
    'V0336003', 'V0009830', 'V0309577', 'V0010144'
)
AND (dmssap.mara.matkl) IN (
    '006*', '007120',    '007130',    '007140',    '007170',    
    '007180',    '007210',    '007220',    '007230',    
    '007250',    '007270',    '007280',    '007290',    
    '007300',    '007320',    '007340',    
    '007370',    '007380',    '007400',    '007420'
)
OR (dmssap.mara.matnr) IN (
    '000000010001767697',     '000000010001870117',     '000000010001870116',     '000000010001870115',    
    '000000010001870114',     '000000010001870113',     '000000010001870112'
)
AND (dmssap.mara.mtart) IN ('HERB', 'HALB', 'ZSTP')
AND (dmssap.ausp.atinn) = '1006'
AND (dmssap.mbew.bwtar) IS NULL;

AND operator has an higher precedence order than OR . AND运算符的优先级比OR更高。 to make your query easily readable use braces () around filter clauses. 为了使您的查询易于阅读,请在过滤器子句周围使用大括号()。

Let's say you want to select records which have certain values in dmssap.mara.matkl or certain values in dmssap.mara.matnr then you can use braces between these blocks to be precise as shown below. 假设您要选择在dmssap.mara.matkl中具有某些值或在dmssap.mara.matnr中具有某些值的记录,然后可以在这些块之间使用大括号,如下所示。

AND (
(dmssap.mara.matkl) IN (
    '006*', '007120',    '007130',    '007140',    '007170',    
    '007180',    '007210',    '007220',    '007230',    
    '007250',    '007270',    '007280',    '007290',    
    '007300',    '007320',    '007340',    
    '007370',    '007380',    '007400',    '007420'
)
OR (dmssap.mara.matnr) IN (
    '000000010001767697',     '000000010001870117',     '000000010001870116',     '000000010001870115',    
    '000000010001870114',     '000000010001870113',     '000000010001870112'
)
)
WHERE (dmssap.mara.ernam) IN (
    'DMSTRAATL', 'V0342628', 'V0343809', 
    'V0336003', 'V0009830', 'V0309577', 'V0010144'
)
AND (dmssap.mara.matkl) IN (
    '006*', '007120',    '007130',    '007140',    '007170',    
    '007180',    '007210',    '007220',    '007230',    
    '007250',    '007270',    '007280',    '007290',    
    '007300',    '007320',    '007340',    
    '007370',    '007380',    '007400',    '007420'
)
OR (dmssap.mara.matnr) IN (
    '000000010001767697',     '000000010001870117',     '000000010001870116',     '000000010001870115',    
    '000000010001870114',     '000000010001870113',     '000000010001870112'
)
AND (dmssap.mara.mtart) IN ('HERB', 'HALB', 'ZSTP')
AND (dmssap.ausp.atinn) = '1006'
AND (dmssap.mbew.bwtar) IS NULL;

The crux is in your WHERE statement. 关键在您的WHERE语句中。 The evaluation order for keywords in the WHERE statement is NOT -> AND -> OR 评估顺序中的关键字WHERE语句NOT - > AND - > OR

What you seem to want is this instead: 您似乎想要的是这个:

WHERE (dmssap.mara.ernam) IN (
    'DMSTRAATL', 'V0342628', 'V0343809', 
    'V0336003', 'V0009830', 'V0309577', 'V0010144'
)
AND (dmssap.mara.matkl) IN ((
    '006*', '007120',    '007130',    '007140',    '007170',    
    '007180',    '007210',    '007220',    '007230',    
    '007250',    '007270',    '007280',    '007290',    
    '007300',    '007320',    '007340',    
    '007370',    '007380',    '007400',    '007420'
)
OR (dmssap.mara.matnr) IN (
    '000000010001767697',     '000000010001870117',     '000000010001870116',     '000000010001870115',    
    '000000010001870114',     '000000010001870113',     '000000010001870112'
))
AND (dmssap.mara.mtart) IN ('HERB', 'HALB', 'ZSTP')
AND (dmssap.ausp.atinn) = '1006'
AND (dmssap.mbew.bwtar) IS NULL;

Notice the two extra brackets around the OR on the AND 请注意AND上OR的两个额外括号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM