SELECT then DELETE right away?

Discussion in 'MySQL' started by thuankkk, Aug 25, 2009.

  1. #1
    Hi,

    I have a problem:

    I wanna SELECT first row of a table (no ORDER), then DELETE it right away before any other process can select it, then continue with another row. There're many processes that do the same job.

    Currently, here's what I do:

    UPDATE `table` SET `selected`=$this_process WHERE `selected`=0 LIMIT 1;// Mark as selected
    SELECT * FROM `table` WHERE `selected`=$this_process;// Fetch the row with ID
    DELETE FROM `table` WHERE `id`=$id;// Then delete it


    Is there any better solution?
    I'm using MySQL.
     
    thuankkk, Aug 25, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    premiumscripts, Aug 25, 2009 IP
    thuankkk likes this.
  3. thuankkk

    thuankkk Active Member

    Messages:
    503
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    95
    #3
    Many thanks to your reply!!!
    I'm reading now :)
     
    thuankkk, Aug 25, 2009 IP