GetYourArse Posted May 21, 2007 Share Posted May 21, 2007 Ok how do i update a row with the lowest id? is this correct or is there a better way? UPDATE table SET title = '$utitle' WHERE id = (SELECT id FROM table ORDER BY id ASC LIMIT 1) Link to comment https://forums.phpfreaks.com/topic/52356-solved-update-lowest-id/ Share on other sites More sharing options...
Wildbug Posted May 21, 2007 Share Posted May 21, 2007 That's fine. Unless your implementation doesn't support subqueries. Link to comment https://forums.phpfreaks.com/topic/52356-solved-update-lowest-id/#findComment-258424 Share on other sites More sharing options...
GetYourArse Posted May 21, 2007 Author Share Posted May 21, 2007 i got this error You can't specify target table 'mytable' for update in FROM clause Link to comment https://forums.phpfreaks.com/topic/52356-solved-update-lowest-id/#findComment-258439 Share on other sites More sharing options...
Wildbug Posted May 21, 2007 Share Posted May 21, 2007 Ah! *Smacks forehead* That's right. I've read that but didn't remember. From MySQL manual: "Currently, you cannot update a table and select from the same table in a subquery." You'll have to use two seperate queries. Either save it in a PHP variable or in a MySQL session variable. Link to comment https://forums.phpfreaks.com/topic/52356-solved-update-lowest-id/#findComment-258442 Share on other sites More sharing options...
Wildbug Posted May 21, 2007 Share Posted May 21, 2007 Or you might be able to do it as follows (untested): UPDATE table SET title = '$utitle' ORDER BY id ASC LIMIT 1 Link to comment https://forums.phpfreaks.com/topic/52356-solved-update-lowest-id/#findComment-258445 Share on other sites More sharing options...
GetYourArse Posted May 21, 2007 Author Share Posted May 21, 2007 thanks didn't know that will be so simple Link to comment https://forums.phpfreaks.com/topic/52356-solved-update-lowest-id/#findComment-258460 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.