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.
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.
Here's some PHP that may help:
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.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); } ?>
Last edited by MaXiMiUS; 06-26-2009 at 01:46 PM.
This thread shouldn't be in the Off-Topic category.
For great installation & servicing of Audio Visual systems & equipmentinc. 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 :-
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 & equipmentinc. 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 :-
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.![]()
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.
As long as there's an index on col, the two ORDER BY clauses won't drag down performance.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;
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.
It is good practice misson to separate subqueries with (), but I think a SELECT INTO statement would be better suited to this.
This would probably be faster because it eliminates the need for a separate query.Code:SELECT * INTO new_table_name FROM old_tablename WHERE condition LIMIT 1; DELETE FROM source WHERE condition LIMIT 1;
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.
The SELECT in INSERT ... SELECT isn't exactly a subquery, so I didn't bother.
MySQL doesn't support SELECT ... INTO.
Both SELECT ... INTO and INSERT ... SELECT require a search and an insert, and will thus perform similarly.
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.