MySQL INSERT INTO with dual condition for IF NOT EXIST

后端 未结 3 1999
粉色の甜心
粉色の甜心 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
          ) ;
    
    0 讨论(0)
  • 2021-01-25 04:14

    Replace

    WHERE NOT EXIST(
    

    With

    WHERE email_addr NOT IN(
    

    EDIT

    Replace

    SELECT email_addr FROM list_email
    WHERE NOT IN
    (
        SELECT email_addr FROM email_addr WHERE email_addr = $post_addr
    ) 
    WHERE NOT IN
    (
        SELECT email_addr FROM list_no_email WHERE email_addr = $post_addr
    )LIMIT 1
    

    with

    SELECT email_addr FROM list_email
    WHERE NOT IN
    (
        SELECT email_addr FROM email_addr WHERE email_addr = $post_addr
        UNION ALL
        SELECT email_addr FROM list_no_email WHERE email_addr = $post_addr
    )
    LIMIT 1
    
    0 讨论(0)
  • 2021-01-25 04:17

    Try something like this:

     INSERT INTO list_email($username.$rowname, fname, lname, list_email) VALUES(?,?,?,?,?)
         SELECT email_addr FROM list_email AS tmp
         WHERE email_addr NOT IN(
             SELECT email_addr FROM list_email WHERE email_addr = $post_addr)
             WHERE email_addr NOT IN (SELECT email_addr FROM list_no_email 
                                      WHERE email_addr = $post_addr)
         LIMIT 1;
    
    0 讨论(0)
提交回复
热议问题