Возможный способ переписать несколько предложений NOT EXISTS в запросе SQL? ` - PullRequest
0 голосов
/ 21 ноября 2018

У меня есть этот оператор SQL, где есть много предложений not exists.Есть ли способ переписать условия и избежать сканирования одной и той же таблицы?

select col1,
       col2,....,colN       
from tab1
join <some join conditions> tab3
where not exists (select null
                  from tab2 p
                  where <some conditions eg: name = 'ABC'> 
                    and tab1.some_col = tab2.some_col)
  and not exists (select null
                  from tab2 p
                  where <some conditions eg: last_name = 'XYZ'> 
                    and tab1.some_col = tab2.some_col)
  and not exists (select null
                  from tab2 p
                  where <some conditions eg: country = 'PQR'> 
                    and tab1.some_col = tab2.some_col)
  and not exists (select null
                  from tab2 p
                  where <similar conditions>
                    and tab1.some_col = tab2.some_col)
  and not exists (select null
                  from tab2 p
                  where <similar conditions>
                    and tab1.some_col = tab2.some_col);

В приведенном выше запросе больше не существует аналогичных способов.поскольку в предложении «не существует» есть одна и та же таблица для проверки, есть ли способ объединить их, не существует в один тип подзапроса.

Ответы [ 2 ]

0 голосов
/ 21 ноября 2018

Вы можете ИЛИ вместе различные условия:

SELECT col1, col2,. .., colN       
FROM tab1 t1
INNER JOIN tab3 t3
    <join conditions>
WHERE NOT EXISTS (SELECT 1
                  FROM tab2 p
                  WHERE
                      (name = 'ABC' OR
                      last_name = 'XYZ' OR
                      country = 'PQR') AND
                      t1.some_col = p.some_col);
0 голосов
/ 21 ноября 2018

Вы можете использовать in ( 'ABC','XYZ',PQR'...), как показано ниже

select col1,
       col2,....,colN       
   from tab1
   join <some join conditions>
  where not exists (select null
                      from tab2 p
                     where <some conditions eg: name in( 'ABC','XYZ',PQR')> 
                     and tab1.some_col = tab2.some_col
                   )
...