需要对Oracle

时间:2018-06-01 09:43:07

标签: sql oracle

我试图一次性取消一个以上的字段,以下是我写的内容。

orig_code send_id ref_no
ABCD001   ABCD001 35RE777
ABCDXXX   ABCDXXX 35RE156
ABCD001   ABCD001 20PS789
ABCDXXX   ABCDXXX 20PS453
ABCD001   ABCD001 30RE745
ABCDXXX   ABCDXXX 55RE741
DBWS001   ABCD001 25PS369
MNKSXXX   ABCDXXX 21PS258
ABCD001   ABCD001 25PS369
ABCDXXX   ABCDXXX 21PS258

SELECT *
FROM T1
WHERE
((orig_code, send_id) NOT IN
                         ( ('ABCD001', 'ABCD001'),
                          ('ABCDXXX', 'ABCDXXX'))
AND (ref_no NOT LIKE '35RE%' OR ref_no NOT LIKE '20PS%'))

我写的查询不会删除上表中的内容。请让我知道我犯了哪些错误。

我正在寻找的结果如下。可能有多种参考格式,我只包括少数几种。我只需要在满足orig_code和send_id字段值的同时删除35RE或20PS开头的那些。

ABCD001   ABCD001 30RE745
ABCDXXX   ABCDXXX 55RE741
DBWS001   ABCD001 25PS369
MNKSXXX   ABCDXXX 21PS258
ABCD001   ABCD001 25PS369
ABCDXXX   ABCDXXX 21PS258

感谢您的协助。

2 个答案:

答案 0 :(得分:1)

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE t1 ( orig_code, send_id, ref_no ) AS
SELECT 'ABCD001', 'ABCD001', '35RE777' FROM DUAL UNION ALL
SELECT 'ABCDXXX', 'ABCDXXX', '35RE156' FROM DUAL UNION ALL
SELECT 'ABCD001', 'ABCD001', '20PS789' FROM DUAL UNION ALL
SELECT 'ABCDXXX', 'ABCDXXX', '25PS453' FROM DUAL UNION ALL
SELECT 'ABCD001', 'ABCD001', '30RE745' FROM DUAL UNION ALL
SELECT 'ABCDXXX', 'ABCDXXX', '55RE741' FROM DUAL UNION ALL
SELECT 'ABCD001', 'ABCD001', '25PS369' FROM DUAL UNION ALL
SELECT 'ABCDXXX', 'ABCDXXX', '21PS258' FROM DUAL;

查询1

SELECT *
FROM   t1
WHERE  (
         (orig_code, send_id) NOT IN (
           ('ABCD001', 'ABCD001'),
           ('ABCDXXX', 'ABCDXXX')
         )
         OR (                             -- OR rather than AND
             ref_no NOT LIKE '35RE%'
         AND ref_no NOT LIKE '20PS%'      -- AND rather than OR
         )
       )

查询2 或者使用NOT(A) AND NOT(B) = NOT( A OR B )NOT(A) OR NOT(B) = NOT( A AND B )

SELECT *
FROM   t1
WHERE  NOT (
         (orig_code, send_id) IN (
           ('ABCD001', 'ABCD001'),
           ('ABCDXXX', 'ABCDXXX')
         )
         AND (
             ref_no LIKE '35RE%'
         OR ref_no LIKE '20PS%'
         )
       )

<强> Results

| ORIG_CODE | SEND_ID |  REF_NO |
|-----------|---------|---------|
|   ABCDXXX | ABCDXXX | 25PS453 |
|   ABCD001 | ABCD001 | 30RE745 |
|   ABCDXXX | ABCDXXX | 55RE741 |
|   ABCD001 | ABCD001 | 25PS369 |
|   ABCDXXX | ABCDXXX | 21PS258 |

答案 1 :(得分:0)

根据你最新的例子,这就足够了:

SELECT *
FROM T1
WHERE ref_no NOT LIKE '35RE%'
  and ref_no NOT LIKE '20PS%'

使用您的数据:

with T1(orig_code, send_id, ref_no) as (
    select 'ABCD001', 'ABCD001', '35RE777' from dual union all
    select 'ABCDXXX', 'ABCDXXX', '35RE156' from dual union all
    select 'ABCD001', 'ABCD001', '20PS789' from dual union all
    select 'ABCDXXX', 'ABCDXXX', '20PS453' from dual union all
    select 'ABCD001', 'ABCD001', '30RE745' from dual union all
    select 'ABCDXXX', 'ABCDXXX', '55RE741' from dual union all
    select 'DBWS001', 'ABCD001', '25PS369' from dual union all
    select 'MNKSXXX', 'ABCDXXX', '21PS258' from dual union all
    select 'ABCD001', 'ABCD001', '25PS369' from dual union all
    select 'ABCDXXX', 'ABCDXXX', '21PS258' from dual
)
SELECT *
FROM T1
WHERE ref_no NOT LIKE '35RE%'
  and ref_no NOT LIKE '20PS%'

ORIG_CODE SEND_ID REF_NO 
--------- ------- -------
ABCD001   ABCD001 30RE745
ABCDXXX   ABCDXXX 55RE741
DBWS001   ABCD001 25PS369
MNKSXXX   ABCDXXX 21PS258
ABCD001   ABCD001 25PS369
ABCDXXX   ABCDXXX 21PS258