代码之家  ›  专栏  ›  技术社区  ›  Bigeyes

满足复杂的条件然后做某事

  •  -1
  • Bigeyes  · 技术社区  · 7 年前

    我有三个场景,如果所有人都满意,那么就做点什么。

    1. select count(*) from schema.member where condition1
    2. select id from schema.ta a, schema.b where a.id=b.id and a.id !='input_id' and a.id in ('M','N'); 它应该为null或不存在。
    3. select member from schema.tc, x schema.tb where condition3 ; 它也应该为null或不存在。

    所以基本上,如果所有3种情况都满足,那么我会做一些事情。

    我想用一个存储过程来做这件事。我的想法是 从每个场景的计数总和中获取一个整数;如果为0,则表示所有人都满意。

    select count(id) from schema.member where condition1 + select count(id) from ... where condition2 + select count(member) from ... where condition3 = 0

    不确定什么是最好的方式,请提供脚本帮助。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Kaushik Nayak    7 年前

    你可以把它们都放进 NOT EXISTS 检查

    DECLARE
         l_flag   INTEGER;
    BEGIN
         SELECT
              CASE
                   WHEN NOT EXISTS (
                        SELECT 1
                        FROM schema.member
                        WHERE condition1 = 'somecondition'
                   ) AND NOT EXISTS (
                        SELECT 1
                        FROM schema.ta a
                        JOIN schema.b ON a.id = b.id --use proper join syntax
                                 WHERE a.id != 'input_id' AND a.id IN ('M','N') 
                                 AND a.id IS NOT NULL -- no nulls as you said.
                   ) AND NOT EXISTS (
                        SELECT 1
                        FROM schema.tc
                        JOIN schema.tb ON condition3 = '<somecondition>'
                        WHERE member IS NOT NULL -- no nulls as you said.
                   ) THEN 1
                   ELSE 0
              END
         INTO l_flag
         FROM dual;
    
         IF
              l_flag = 1
         THEN
              do_something;
         END IF;
    END;
    /
    

    推荐文章