SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Is there a way to use one query instead of two to check if a row is already in there, overwrite it (UPDATE) and if it's not there, push it in (INSERT)?

    And if I need 2 queries, which is the most efficient way to know if a row already exists in the table?

    Thanks,
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  2. #2
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SQL doesn't have any IF THEN ELSE statements. No conditional code.

    So to answer your first question. No.

    Second Question: Just query it. Then do an IF variable (results) exists statement.



    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  3. #3
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's what I normally do, but too bad if there is no better way!
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  4. #4
    SitePoint Enthusiast MAC's Avatar
    Join Date
    Feb 2000
    Location
    Somewhere on the heart of Dixie
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In MySQL, yes there is!!!!

    REPLACE works exactly like INSERT, except that if an old record in the table has the same value as a new record on a unique index,the old record is deleted before the new record is inserted.
    --Daniel
    Fan of PHP, Photoshop, and Macs
    Andrew's Cars - My little brother's cars for Need for Speed 3

  5. #5
    SitePoint Guru
    Join Date
    Jan 2001
    Location
    Alkmaar, Netherlands
    Posts
    710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think Son's questiuon is coming from not knowing the unique index,
    if he knows unique index (it means he knows record in the table) then
    he will update
    else
    he will insert


  6. #6
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was going along the lines of member registration. I don't think he wants to delete the old value if it is the same as the submitted value, if so he'd just use an update query and woulda never had to ask.

    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  7. #7
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh yeah, that REPLACE is exactly what I needed
    Thank you all guys and sorry for any misunderstanding.
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy


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
  •