+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Does Anyone Know SQL

  1. #1
    incman is offline x10Hosting Member incman is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    2

    Does Anyone Know SQL

    Hi

    Does anyone know sql enough to lend me a hand.

    I just want to know how I can simply locate the last record in a table and the move that record into a new table.

    I've been trying for a couple of weeks now and no success.

    Thanks in advance.

  2. #2
    MaXiMiUS is offline x10Hosting Member MaXiMiUS is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    9

    Re: Does Anyone Know SQL

    Here's some PHP that may help:
    Code:
    <?php
    $link = mysql_connect("host","user","pass");
    mysql_select_db("db_name",$link);
    $selectSQL = "SELECT * FROM `sometable` ORDER BY `order_column` DESC LIMIT 1;"; // Remove or change the LIMIT 1 depending on how many rows you would like to move to the other table
    $query = mysql_query($selectSQL,$link);
    while($row = mysql_fetch_object($query)){
        $deleteSQL = "DELETE FROM `sometable` WHERE `identifying_column` = '".($row->identifying_column)."';";
        $updateSQL = "INSERT INTO `othertable`(`identifying_column`,`order_column`,`column3`,`column4`) VALUES('".($row->identifying_column)."','".($row->order_column)."','".($row->column3)."','".($row->column4)."');";
        mysql_query($deleteSQL,$link);
        mysql_query($updateSQL,$link);
    }
    ?>
    Edit: What you're asking to do -could- be done with only SQL, but I don't think it could be done in a single query. You'd basically have to do the same thing the PHP code is doing here.
    Last edited by MaXiMiUS; 06-26-2009 at 01:46 PM.

  3. #3
    zen-r's Avatar
    zen-r is offline Lord Of The Keys zen-r is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Location,Location. Nothing else matters ....apparently.
    Posts
    1,937

    Re: Does Anyone Know SQL

    This thread shouldn't be in the Off-Topic category.
    For great installation & servicing of Audio Visual systems & equipment
    inc. LCD & Plasma Screens, Loudspeakers, Projectors, Aerials & Satellite Dishes, Lighting effects & controllers, Hifi, Amplifiers, Surround Sound, Home Cinema & Video etc
    -: based around Plymouth, Devon, or anywhere in the southwest of the UK, visit :-

  4. #4
    Kayos's Avatar
    Kayos is offline Community Advocate Kayos is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Pittsburgh, Pennsylvania
    Posts
    987

    Re: Does Anyone Know SQL

    Quote Originally Posted by zen-r View Post
    This thread shouldn't be in the Off-Topic category.
    Reporting the post helps us moderators put threads in the correct place.

    -Moved to Programming Help-

  5. #5
    merrillmck is offline x10 Sophmore merrillmck is an unknown quantity at this point
    Join Date
    Jun 2009
    Location
    Florida, USA
    Posts
    134

  6. #6
    zen-r's Avatar
    zen-r is offline Lord Of The Keys zen-r is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Location,Location. Nothing else matters ....apparently.
    Posts
    1,937

    Re: Does Anyone Know SQL

    Quote Originally Posted by Kayos View Post
    Reporting the post helps us moderators put threads in the correct place.

    -Moved to Programming Help-
    I've always wondered about that.

    So it's OK to use the "Report Post" icon for that then?

    It says "Note: This is ONLY to be used to report spam, advertising messages, and problematic (harassment, fighting, or rude) posts." & since this doesn't come under any of those categories, I didn't want to use the Report Post icon if I wasn't supposed to.

    What about threads that we think should be closed (not because they're spam or abusive, but for other reasons -eg(1) the poster is starting duplicate threads of the same question/ topic, or eg(2) because an original offer has closed but noobs keeps thinking the offer is still running & keep adding new posts to the end of a thread)? Should the icon be used then also? Or should we just wait for a Mod to find those threads themselves?
    Last edited by zen-r; 06-30-2009 at 05:23 AM.
    For great installation & servicing of Audio Visual systems & equipment
    inc. LCD & Plasma Screens, Loudspeakers, Projectors, Aerials & Satellite Dishes, Lighting effects & controllers, Hifi, Amplifiers, Surround Sound, Home Cinema & Video etc
    -: based around Plymouth, Devon, or anywhere in the southwest of the UK, visit :-

  7. #7
    Kayos's Avatar
    Kayos is offline Community Advocate Kayos is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Pittsburgh, Pennsylvania
    Posts
    987

    Re: Does Anyone Know SQL

    Quote Originally Posted by zen-r View Post
    I've always wondered about that.

    So it's OK to use the "Report Post" icon for that then?

    It says "Note: This is ONLY to be used to report spam, advertising messages, and problematic (harassment, fighting, or rude) posts." & since this doesn't come under any of those categories, I didn't want to use the Report Post icon if I wasn't supposed to.

    What about threads that we think should be closed (not because they're spam or abusive, but for other reasons -eg(1) the poster is starting duplicate threads of the same question/ topic, or eg(2) because an original offer has closed but noobs keeps thinking the offer is still running & keep adding new posts to the end of a thread)? Should the icon be used then also? Or should we just wait for a Mod to find those threads themselves?
    It's fine to report a message saying that a thread is in the wrong place, especially if you see a thread in the same place for days on end. I try to move everything eventually but I can't be here all day long.


    And with reporting closing threads:

    1. Duplicate threads can be considered spam so yes you can report them.

    2. Those type of threads are solely at the discretion of the moderators.

    If you would like to discuss this more please PM me.

  8. #8
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Does Anyone Know SQL

    Quote Originally Posted by MaXiMiUS View Post
    Edit: What you're asking to do -could- be done with only SQL, but I don't think it could be done in a single query. You'd basically have to do the same thing the PHP code is doing here.
    It can be done in two by using INSERT ... SELECT followed by DELETE, but (as you surmise) not one.

    This looks like it needs transactions to prevent duplicates in the tables if the DELETE fails to execute.

    Code:
    START TRANSACTION;
    INSERT INTO target SELECT * FROM source ORDER BY col DESC LIMIT 1;
    DELETE FROM source ORDER BY col DESC LIMIT 1;
    COMMIT;
    As long as there's an index on col, the two ORDER BY clauses won't drag down performance.
    Last edited by misson; 06-30-2009 at 09:53 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  9. #9
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: Does Anyone Know SQL

    Quote Originally Posted by misson View Post
    It can be done in two by using INSERT ... SELECT followed by DELETE, but (as you surmise) not one.

    This looks like it needs transactions to prevent duplicates in the tables.

    Code:
    START TRANSACTION;
    INSERT INTO target SELECT * FROM source ORDER BY col DESC LIMIT 1;
    DELETE FROM source ORDER BY col DESC LIMIT 1;
    COMMIT;
    As long as there's an index on col, the two ORDER BY clauses won't drag down performance.
    It is good practice misson to separate subqueries with (), but I think a SELECT INTO statement would be better suited to this.
    Code:
    SELECT *
    INTO new_table_name
    FROM old_tablename
    WHERE condition
    LIMIT 1;
    
    DELETE
    FROM source
    WHERE condition
    LIMIT 1;
    This would probably be faster because it eliminates the need for a separate query.

    About the transactions I am not sure. I thought that it required logic in PHP in order for you to decide whether to issue a ROLLBACK command when you start a transaction? Or maybe the BEGIN - ROLLBACK - COMMIT syntax is specific to MySQL.

    Make sure you have the appropraite table type (InnoDB) for transactions which must be specified in the table's creation. The default MyISAM will not work with transactions.
    Code:
    TABLE (
    ....
    ) type=innodb;
    Last edited by Twinkie; 06-30-2009 at 05:40 PM.

  10. #10
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Does Anyone Know SQL

    Quote Originally Posted by Twinkie View Post
    It is good practice misson to separate subqueries with (),
    The SELECT in INSERT ... SELECT isn't exactly a subquery, so I didn't bother.

    Quote Originally Posted by Twinkie View Post
    but I think a SELECT INTO statement would be better suited to this.
    MySQL doesn't support SELECT ... INTO.

    Quote Originally Posted by Twinkie View Post
    This would probably be faster because it eliminates the need for a separate query.
    Both SELECT ... INTO and INSERT ... SELECT require a search and an insert, and will thus perform similarly.

    Quote Originally Posted by Twinkie View Post
    About the transactions I am not sure. I thought that it required logic in PHP in order for you to decide whether to issue a ROLLBACK command when you start a transaction? Or maybe the BEGIN - ROLLBACK - COMMIT syntax is specific to MySQL.[/CODE]
    A ROLLBACK is only sent to discard the changes introduced by a transaction; it shouldn't be needed here. Since the OP never mentioned anything other than SQL, I didn't bother with any other language.


    @OP: another reason transactions are necessary here is that when you use a single query to copy a row from one table to another, you don't get a unique field that you can use in a DELETE query. Instead, you have to use the same selection clause in both statements. Another query could execute between the INSERT and DELETE, changing the row matched by the selection clause and thus causing the wrong row to get deleted.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Microsoft SQL Server (a rant)
    By merrillmck in forum Programming Help
    Replies: 6
    Last Post: 07-08-2009, 10:43 AM
  2. sql database does not restore
    By softwork in forum Free Hosting
    Replies: 3
    Last Post: 05-20-2008, 06:47 AM
  3. Cannot upload SQL Data
    By pasacom in forum Free Hosting
    Replies: 5
    Last Post: 02-25-2008, 11:24 PM
  4. Sql?
    By SLIForum in forum Scripts & 3rd Party Apps
    Replies: 4
    Last Post: 12-03-2007, 01:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers