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 > Database Server Software > MySQL > Custom ordering??

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-03-07, 03:16
uraknai uraknai is offline
Registered User
 
Join Date: Mar 2006
Posts: 41
Custom ordering??

Hi,

is there a way to make the results order in a custom way. So, rather than alphabetically or numeriacally you can set the order in which the results appear.

So, for example, if one column had only the values APPLE, ORANGE, BANANA, PEAR I could say arrange the results in the custom order [ORANGE, BANANA, APPLE, PEAR].

Thanks
Reply With Quote
  #2 (permalink)  
Old 09-03-07, 03:43
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Code:
DECLARE @fruits table (fruitName varchar(10), sortOrder int)

INSERT INTO @fruits(fruitName) VALUES('Orange')
INSERT INTO @fruits(fruitName) VALUES('Apple')
INSERT INTO @fruits(fruitName) VALUES('Pear')
INSERT INTO @fruits(fruitName) VALUES('Banana')

SELECT fruitName
     , CASE fruitName
         WHEN 'Orange' THEN 1
         WHEN 'Banana' THEN 2
         WHEN 'Apple'  THEN 3
         ELSE 4
       END As 'sortOrder'
FROM   @fruits
ORDER
   BY  x ASC

--OR--

UPDATE @fruits SET sortOrder = 1 WHERE fruitname = 'Orange'
UPDATE @fruits SET sortOrder = 2 WHERE fruitname = 'Banana'
UPDATE @fruits SET sortOrder = 3 WHERE fruitname = 'Apple'
UPDATE @fruits SET sortOrder = 4 WHERE fruitname = 'Pear'

SELECT fruitName
     , sortOrder
FROM   @fruits
ORDER
   BY  sortOrder ASC
Personally I'd go for the second option (storing the sort order).
Hard-coding stuff is rubbish
__________________
George
Twitter | Blog

Last edited by gvee; 09-03-07 at 03:51. Reason: minor amendments to syntax
Reply With Quote
  #3 (permalink)  
Old 09-03-07, 03:45
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
use something like a product type table and defien your sort rules there, the product type can refer to itself to give a tree hierarchy
eg
Product Type
ID autonumber PK
Desc Text
ParentID FK to ID in ProductType
sample data...
1 Fresh Produce <NULL>
5 Fruit 1
3 Vegetables 1
10 Root Vegetable 3
11 Leaf Vegetables 3
9 Potatoes 10
8 King Edward 9
.....

or you can allocate a column say called SortSequence to the product column

Product
ID-autonumber PK
Desc - text
SortSequence
eg
10 APPLE 5
14 ORANGE 0
18 BANANA 4
201 PEAR 99
then using the sortseq as a key the will come out int he preferred order. You could chose a mix to give product categories and sort sequence, its optional if you make the sort sequence unique (persoanlly Iwouldn't, I'd use sortseq and (say) desc so that if a sort sequence isn't provided they will come out in alpha order.
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #4 (permalink)  
Old 09-03-07, 03:47
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
A product type table..?
__________________
George
Twitter | Blog
Reply With Quote
  #5 (permalink)  
Old 09-03-07, 05:16
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by uraknai
So, for example, if one column had only the values APPLE, ORANGE, BANANA, PEAR I could say arrange the results in the custom order [ORANGE, BANANA, APPLE, PEAR].
Code:
ORDER
    BY FIELD(myfruits,'orange','banana','apple','pear')
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 09-03-07, 05:27
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Ooh I did not know that!
mySQL proprietary?
__________________
George
Twitter | Blog
Reply With Quote
  #7 (permalink)  
Old 09-03-07, 05:35
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by georgev
mySQL proprietary?
yeppir, but useful, no?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 09-03-07, 05:51
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Yep, no doubting that...
But it's still technically hard-coding the solution!
__________________
George
Twitter | Blog
Reply With Quote
  #9 (permalink)  
Old 09-03-07, 06:29
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by georgev
Yep, no doubting that...
But it's still technically hard-coding the solution!
that is true

but hardcoding is not unfailingly always a bad solution

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 09-03-07, 07:09
uraknai uraknai is offline
Registered User
 
Join Date: Mar 2006
Posts: 41
Thanks guys.

I used the
ORDER
BY FIELD(myfruits,'orange','banana','apple','pear')

method since it was the simplest to integrate into my existing code. In the future I'll probably use georgev's method of making a sort order column.

Cheers
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