trying to insert with select from same table

Discussion in 'MySQL' started by neha2011, May 4, 2011.

  1. #1
    INSERT INTO mailing_lists
    SELECT
      business_id
    , email_address
    , first_name
    FROM mailing_lists
    WHERE business_id = 101
    
    Code (markup):

    But how to I build in an ON DUPLICATE KEY UPDATE clause?
     
    neha2011, May 4, 2011 IP
  2. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
  3. ruvenf

    ruvenf Peon

    Messages:
    19
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Do you mean some thing like this?
    INSERT INTO mailing_lists (business_id, email_address, first_name) 
    SELECT
      business_id
    , email_address
    , first_name
    FROM mailing_lists
    WHERE business_id = 101
    ON DUPLICATE KEY UPDATE email_address= (select email_address FROM mailing_lists
    WHERE business_id = 101), (first_name= select first_name FROM mailing_lists
    WHERE business_id = 101), business_id = 101
    Code (markup):
     
    ruvenf, May 4, 2011 IP