MySQL INSERT INTO with dual condition for IF NOT EXIST

后端 未结 3 2006
粉色の甜心
粉色の甜心 2021-01-25 03:18

I am trying to insert a new record if the email address does not exist in list_email.email_addr AND not exist in list_no_email.email_addr

INSERT INTO list_email         


        
3条回答
  •  逝去的感伤
    2021-01-25 04:12

    INSERT INTO list_email
      (fname, lname, email_addr)
    SELECT ins.*
    FROM
      ( SELECT 'bob' AS fname, 
               'schmoe' AS lname, 
               'bogus@bogus.com' AS email_addr
        FROM dual
      UNION ALL                               -- for every row to be inserted
        SELECT 'mary', 'lamb', 'hoe@me.com'
        FROM dual 
                                              -- add another UNION ALL .. FROM dual
      ) AS ins
    WHERE NOT EXISTS
          ( SELECT 1 
            FROM email_addr AS e 
            WHERE e.email_addr = ins.email_addr
          )
      AND NOT EXISTS
          ( SELECT 1 
            FROM list_no_email AS ne 
            WHERE ne.email_addr = ins.email_addr
          ) ;
    

提交回复
热议问题