首页 > 解决方案 > 如何将左连接与第二个选择语句一起使用?

问题描述

我的应用中有查询 1。但是,我面临NOT IN(太慢)的性能问题。所以,我试图用LEFT JOIN. 如何编写查询?还有更好的选择吗?

查询 1 =

SELECT COUNT(id) 
FROM Payment 
WHERE gl_posting LIKE :glposting 
  AND id NOT IN (SELECT PAYMENT_ID 
                 FROM UNPOST_REQUEST_P 
                 WHERE status NOT LIKE :status );

我试过了

SELECT COUNT(id) 
FROM Payment 
WHERE gl_posting LIKE :glposting 
  AND id LEFT JOIN (SELECT PAYMENT_ID 
                    FROM UNPOST_REQUEST_P 
                    WHERE status NOT LIKE :status );

但是,我得到一个错误

[42000][920] ORA-00920: 无效的关系运算符

标签: sqloracle

解决方案


尝试not exists提高性能

select 
    count(id) 
FROM Payment p
where gl_posting like :glposting 
and not exists (
    select 
        payment_id
    from UNPOST_REQUEST_P ur
    where p.id = ur.payment_id
    and status not like :status
)

此外,您应该首先学习如何使用左连接(或任何连接)。


推荐阅读