dbTalk Databases Forums  

fetch rows order in user-defined order

comp.databases comp.databases


Discuss fetch rows order in user-defined order in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Melon
 
Posts: n/a

Default fetch rows order in user-defined order - 12-20-2003 , 02:35 PM






I'm starting to work with mysql (but I think my post applies to other
databases as well) and I've come across a problem - I've read in several
sources that the order of retrieved rows by a SELECT statement is
undefined unless you use GROUP BY or ORDER BY. Now I need to be able to
fetch the rows in an order defined by me or the web site's owner.
Imagine that each row represents one product in a shop. Now I need to
let the shop owner decide in which order the products appear on a web
page. He needs to be able to freely change this order at any time and
move any product to any position he wants. What would be the easiest way
to accomplish this in a database knowing I can't change the row order? I
will be grateful for any ideas.

Melon.


Reply With Quote
  #2  
Old   
Jim Kennedy
 
Posts: n/a

Default Re: fetch rows order in user-defined order - 12-20-2003 , 05:21 PM






You would have to have a column that specifies the order and order on that.
Possibly call the column position.
(as he changes the display order you change the value in the position column
to match the order the customer wants)
Jim
"Melon" <maikel7 (AT) poczta (DOT) onet.pl> wrote

Quote:
I'm starting to work with mysql (but I think my post applies to other
databases as well) and I've come across a problem - I've read in several
sources that the order of retrieved rows by a SELECT statement is
undefined unless you use GROUP BY or ORDER BY. Now I need to be able to
fetch the rows in an order defined by me or the web site's owner.
Imagine that each row represents one product in a shop. Now I need to
let the shop owner decide in which order the products appear on a web
page. He needs to be able to freely change this order at any time and
move any product to any position he wants. What would be the easiest way
to accomplish this in a database knowing I can't change the row order? I
will be grateful for any ideas.

Melon.




Reply With Quote
  #3  
Old   
Melon
 
Posts: n/a

Default Re: fetch rows order in user-defined order - 12-21-2003 , 05:01 AM



Jim Kennedy wrote:
Quote:
You would have to have a column that specifies the order and order on that.
Possibly call the column position.
(as he changes the display order you change the value in the position column
to match the order the customer wants)
Jim
This sounds good, thanks!



Reply With Quote
  #4  
Old   
--CELKO--
 
Posts: n/a

Default Re: fetch rows order in user-defined order - 12-22-2003 , 01:05 PM



Quote:
I've read in several sources that the order of retrieved rows by a
SELECT statement is undefined unless you use GROUP BY or ORDER BY.

Not quite right. GROUP BY has no implied ordering; some of the
earlier SQL engines did grouping with a sort and that was a side
effect. Other products use hashing and you get the smaller buckets
back first.

The ORDER BY clause is part of a cursor and not a query; cursors
convert a result set into a sequential file structure, so that
ordering makes sense.

Quote:
Imagine that each row represents one product in a shop. Now I need
to let the shop owner decide in which order the products appear on a
web page. <<

Create a table with two columns like (upc, sort_order), join it to the
target table and do an ORDER BY on the sort_order column. The
sort_order column should be an integer. There are some other posting
that give you the code to move things around in the list, but they are
just simple algebra and a CASE expression in an UPDATE statement.


Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.