If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > Saving the order of something

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-27-12, 15:42
Hans Gruber Hans Gruber is offline
Registered User
 
Join Date: Jan 2012
Posts: 2
Saving the order of something

Hey there,

the user chooses 0 to n entries in a specified order and i've to save said order of the entries in the user table. My approach of saving the order in a single string (1,5,3,2) worked, but I think that's not exactly considered best practice.

Table User: (int ID; string username; string order) e.g: "1";"Hans";"1,2,3,4"
Table Entries: (int ID; string content) e.g. "1";"Fruityloops"

Any ideas how to improve this?
Reply With Quote
  #2 (permalink)  
Old 01-27-12, 16:38
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Hans Gruber View Post
Any ideas how to improve this?
depends entirely on what you want to do with it

i.e. database design means squat without knowing the SQL that will be run against it
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 01-27-12, 16:53
Hans Gruber Hans Gruber is offline
Registered User
 
Join Date: Jan 2012
Posts: 2
To be specific: The user customize the menu of a webpage, he can arrange the order of the menu items and close any of them. In order to print the menu for the current user, i need to get the order (Select Order From User Where Username = $logged_in), after that i select the actual context of every displayed Item (Select * From Entities where ID=$id).
Reply With Quote
  #4 (permalink)  
Old 01-27-12, 17:42
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
looks like you're okay with running two queries, then ... which is good

be careful naming the string in the users table, because ORDER is a reserved word

the second query will use IN instead of equals, but you will still ahve to sequence them properly

if you're using mysql, you can use the handy-dandy FIELD function in your ORDER BY clause
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 01-27-12, 19:55
futurity futurity is offline
Registered User
 
Join Date: May 2008
Posts: 270
Why not a 3rd table?

user_entry: user_id*, entry_id*, entry_sequence

Then to select the entries, just do a join between entry and user_entry ordering by entry_sequence.

If you want to be really strict, you can put a separate unique constraint on (user_id, entry_sequence), though this can make updating the sequence a bit of a nuisance.
Reply With Quote
  #6 (permalink)  
Old 01-31-12, 09:46
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,246
consider storing the ORDER BY clause as a variable against the users current login.

eg
futurity logontime transaction ID ORDER BY manufacturer, type, price DESC
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On