Kieran Menor Posted August 3, 2009 Share Posted August 3, 2009 Here is my table: TABLE `categories` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `order` int(10) unsigned NOT NULL, PRIMARY KEY (`id`) ) Now, consider this query: INSERT INTO `categories` (`name`, `order`) VALUES ('Test', ????) I want the "order" field to have the same value as the ID field will get when the row is inserted. Is this possible? Edit: And if so, how. Link to comment https://forums.phpfreaks.com/topic/168650-using-last-insert-id-in-query/ Share on other sites More sharing options...
Maq Posted August 3, 2009 Share Posted August 3, 2009 I think you need, mysql_insert_id. Link to comment https://forums.phpfreaks.com/topic/168650-using-last-insert-id-in-query/#findComment-889684 Share on other sites More sharing options...
Kieran Menor Posted August 3, 2009 Author Share Posted August 3, 2009 Well, I would rather do it at insert time, but if that's not possible I suppose I can just update the row afterwards. Link to comment https://forums.phpfreaks.com/topic/168650-using-last-insert-id-in-query/#findComment-889689 Share on other sites More sharing options...
Maq Posted August 3, 2009 Share Posted August 3, 2009 Well, I would rather do it at insert time, but if that's not possible I suppose I can just update the row afterwards. I see. Sorry, I'm not aware of a way to do this in a single query. Link to comment https://forums.phpfreaks.com/topic/168650-using-last-insert-id-in-query/#findComment-889694 Share on other sites More sharing options...
angelcool Posted August 3, 2009 Share Posted August 3, 2009 Use LAST_INSERT_ID() http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html I will check the offset, what I mean is that you might be getting the id of the row inserted before the query. Cheers! Link to comment https://forums.phpfreaks.com/topic/168650-using-last-insert-id-in-query/#findComment-889888 Share on other sites More sharing options...
Maq Posted August 3, 2009 Share Posted August 3, 2009 That would still require two queries... Link to comment https://forums.phpfreaks.com/topic/168650-using-last-insert-id-in-query/#findComment-889891 Share on other sites More sharing options...
angelcool Posted August 3, 2009 Share Posted August 3, 2009 This will work: LAST_INSERT_ID()+1 Too much for me tho!! Link to comment https://forums.phpfreaks.com/topic/168650-using-last-insert-id-in-query/#findComment-889901 Share on other sites More sharing options...
angelcool Posted August 3, 2009 Share Posted August 3, 2009 This is my understanding: LAST_INSERT_ID() & mysql_insert_id() This 2 guys work on a connection basis. If a host connects to MySQL and inserts a record, you can get the query ID using LAST_INSERT_ID() & mysql_insert_id() after executing the query. If another hosts connects an execute LAST_INSERT_ID() & mysql_insert_id() will get 0, this is because no record has been inserted with this connection. The only solution I can thing of to do what the OP wants, is to run a separate query or perhaps create a transaction. Using two queries: INSERT INTO `categories` (`name`) VALUES ('Test'); UPDATE `categories` SET `order`=LAST_INSERT_ID() WHERE Id=LAST_INSERT_ID(); Angel Link to comment https://forums.phpfreaks.com/topic/168650-using-last-insert-id-in-query/#findComment-889920 Share on other sites More sharing options...
Maq Posted August 3, 2009 Share Posted August 3, 2009 The only solution I can thing of to do what the OP wants, is to run a separate query or perhaps create a transaction. That's no what the OP wants. Please read the previous posts. Link to comment https://forums.phpfreaks.com/topic/168650-using-last-insert-id-in-query/#findComment-889940 Share on other sites More sharing options...
angelcool Posted August 3, 2009 Share Posted August 3, 2009 The OP wants to do this: INSERT INTO `categories` (`name`) VALUES ('Test'); UPDATE `categories` SET `order`=LAST_INSERT_ID() WHERE Id=LAST_INSERT_ID(); with a single query. The only solution I can think of to achieve the final result that OP will get, is to run a separate query or perhaps create a transaction. :'( Link to comment https://forums.phpfreaks.com/topic/168650-using-last-insert-id-in-query/#findComment-889976 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.