| | |||||
| ||||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | THREAD STARTER #1 (permalink) |
| NamePros Regular Join Date: Mar 2006 Location: USA
Posts: 497
![]() ![]() | Update ID in MySQL? Let's say I have this column in a database: ID | Name 2 | Snike Is it possible to have a php script update the ID number to the next available ID number? If in the example above there were 2 more column and the largest ID being 4, the script would update the ID from 2 to 5. Can I do this? Thanks in advance.
Last edited by snike; 11-03-2009 at 05:44 PM.
|
| | |
| | #7 (permalink) |
| Forum Moderator ![]() Join Date: Aug 2006 Location: USA
Posts: 2,152
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | You should be able to do this as a subquery (mysql 5.0 and higher) but you have to do some extra work because you can't use a subquery on the same table to provide a value to an update statement: Something like this might work ... ( WARNING: not tested and since I rarely write complex sql statements any more there's a good chance it's not quite right or there's something stupidly easy that I'm not thinking of !)????: NamePros.com http://www.namepros.com/programming/620935-update-id-in-mysql.html Code: Update table set id = (select * from (select (max(id) + 1) from table group by id) as i) WHERE id = 2; Or here's a really easy alternative: Get all the values from the old record (except ID) Insert as a new record Delete the record with the old ID
__________________ Enlytend Solutions - Internet marketing and web development Was my advice helpful? Please consider a small donation to the National Canine Cancer Foundation - a tax deductible 501(c)(3)that directly funds cancer research
Last edited by enlytend; 11-06-2009 at 05:52 AM.
|
| | |