943,922 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Unsolved
  • Views: 1166
  • MySQL RSS
Jan 7th, 2009
0

way to inserting data into a MYSQL table data in an ordered way

Expand Post »
Hello all,

I'd like to know if there is a way to insert into a MYSQL table data in an ordered way?
Right now as a workaround I've literally made a table of questions with questionID as primary key, nextQuestion and prevQuestion pointing to other questionIDs. Basically I've made a two way linked list from scratch. Is there anything built into MYSQL that implements this?

Thanks in advance.
Similar Threads
Reputation Points: 7
Solved Threads: 2
Light Poster
komrad is offline Offline
39 posts
since Sep 2006
Jan 9th, 2009
0

Re: way to inserting data into a MYSQL table data in an ordered way

I don't know if there is anything built into MySQL to do this. It's been a while since I've had to 'track' autoincrement values, but that's where you can look. But you can get close using autoincrement.

Basically, you create a table where the question_id is an auto-increment value. You would insert the first question using, for example, -1 for the previous_id and -1 for the next_id. For the rest of the questions, you would insert the question using LAST_INSERT_ID() for the previous_id and -1 for the next_id, then you would update the record with question_id=LAST_INSERT_ID()-1 and set its next_id to LAST_INSERT_ID().

This will work as long as nothing else is inserting rows into the table. I believe autoincrement is guaranteed to increment by one with each record inserted. You could create many separate lists of questions as long as you don't interrupt the insert process. (I don't know if there's a way to reset the LAST_INSERT_ID value.)

Want pseudo-pseudo code?

MySQL Syntax (Toggle Plain Text)
  1. LOCK TABLE question_list;
  2. INSERT INTO question_list
  3. SET TEXT='The text of the question',
  4. previous_id='-1',
  5. next_id='-1';
  6.  
  7. INSERT INTO question_list
  8. SET TEXT='Inside question text',
  9. previous_id=LAST_INSERT_ID(),
  10. next_id=-1;
  11. UPDATE question_list
  12. SET next_id=LAST_INSERT_ID()
  13. WHERE question_id=LAST_INSERT_ID()-1;
  14.  
  15. INSERT INTO question_list
  16. SET TEXT='Inside question text',
  17. previous_id=LAST_INSERT_ID(),
  18. next_id=-1;
  19. UPDATE question_list
  20. SET next_id=LAST_INSERT_ID()
  21. WHERE question_id=LAST_INSERT_ID()-1;
  22.  
  23. INSERT INTO question_list
  24. SET TEXT='Last question text',
  25. previous_id=LAST_INSERT_ID(),
  26. next_id=-1;
  27. UPDATE question_list
  28. SET next_id=LAST_INSERT_ID()
  29. WHERE question_id=LAST_INSERT_ID()-1;
  30. UNLOCK TABLES;
So this isn't built into MySQL, but I think it accomplishes what you want to do.
Reputation Points: 51
Solved Threads: 35
Posting Whiz in Training
Fest3er is offline Offline
238 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MySQL Forum Timeline: excel to mysql
Next Thread in MySQL Forum Timeline: Distinct random rows





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC