SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    Provo, UT
    Posts
    858
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Using the results of a SELECT to perform an UPDATE

    I read in the mySQL manual, "Currently, you cannot update a table and select from the same table in a subquery." So what is the best way to take the user IDs from this first query...

    Code:
    SELECT uID 
    FROM users
    WHERE sponsorID = 717 
       and memberType = 'No' 
       and uID in (select uID from campaigns where type = 'Enrollment' and answer in ('Contact', 'Purchase') 
       and status = 'Undiscovered');
    ...and then turn right around and update the campaigns table with this

    Code:
    update campaigns set status = 'Discovered' where uID in (the uIDs from the result of the first query)
    Thanks!
    Convert your dollars into silver coins. www.convert2silver.com

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,267
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    try this --
    Code:
    UPDATE campaigns 
    INNER
      JOIN users
        ON users.uID = campaigns.uID  
       AND users.sponsorID = 717 
       AND users.memberType = 'No' 
       AND users.status = 'Undiscovered'
    SET    campaigns.status = 'Discovered' 
     WHERE campaigns.type = 'Enrollment' 
       AND campaigns.answer in ('Contact', 'Purchase')
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    Provo, UT
    Posts
    858
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you Rudy!
    Convert your dollars into silver coins. www.convert2silver.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •