SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
-
Dec 10, 2008, 14:45 #1
Auto increment based on a column value?
I did a bit of searching but couldn't find anything, so:
If I have a table structure similar to this:
Code:table: productImages id filename productId order 1 10234.jpg 12 1 2 13702.jpg 12 2 3 23674.jpg 12 3 4 98373.jpg 15 1 5 44126.jpg 15 2
As an example, if I insert another record with filename = 12345.jpg and productId = 12, then it would set order = 4 based on the previous three entries for that product?
cheers
-
Dec 10, 2008, 14:51 #2
- Join Date
- Feb 2008
- Posts
- 655
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
If I am not mistaken, auto-increment fields must also be unique. Somebody will be able to confirm or deny my suspicions.
-
Dec 10, 2008, 21:18 #3
hmm probably right, ah well worth a shot
cheers
-
Dec 10, 2008, 22:20 #4
-
Dec 10, 2008, 23:52 #5
- Join Date
- Jul 2002
- Location
- Toronto, Canada
- Posts
- 39,347
- Mentioned
- 63 Post(s)
- Tagged
- 3 Thread(s)
-
Dec 11, 2008, 18:24 #6
Thanks guys you put me on the right track to figuring it out
Here's my solution if anyone is interested
Code:INSERT INTO productImages(order) SELECT (MAX(order)+1) FROM productImages;
-
Dec 11, 2008, 18:50 #7
- Join Date
- Jul 2002
- Location
- Toronto, Canada
- Posts
- 39,347
- Mentioned
- 63 Post(s)
- Tagged
- 3 Thread(s)
no no noooooo.....
the "select max" technique has several deep flaws
and besides, you aren;t getting a new number by product, that just gets you a new number overall
did you look up the page in da manual as i suggested? they give a very clear example of exactly what you want
-
Dec 12, 2008, 13:40 #8
- Join Date
- Dec 2005
- Posts
- 982
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I love this place. I never knew you oculd do that!
MySQL v5.1.58
PHP v5.3.6
Bookmarks