Bluehost.com Web Hosting $6.95

ORDER BY with special order scheme?

This is a discussion on ORDER BY with special order scheme? within the MySQL Database forums, part of the Database Forums category; Is it possible to order the result by a given list? E.g. I have a "type" column ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-27-2007
Frank Buss
 
Posts: n/a
Default ORDER BY with special order scheme?

Is it possible to order the result by a given list? E.g. I have a "type"
column and I want to return first the records with type 5, then with type
9, then with type 2 etc.

--
Frank Buss, fb@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
Reply With Quote
  #2 (permalink)  
Old 07-27-2007
strawberry
 
Posts: n/a
Default Re: ORDER BY with special order scheme?

On 27 Jul, 08:21, Frank Buss <f...@frank-buss.de> wrote:
> Is it possible to order the result by a given list? E.g. I have a "type"
> column and I want to return first the records with type 5, then with type
> 9, then with type 2 etc.
>
> --
> Frank Buss, f...@frank-buss.dehttp://www.frank-buss.de,http://www.it4-systems.de


see FIELD

Reply With Quote
  #3 (permalink)  
Old 07-27-2007
Frank Buss
 
Posts: n/a
Default Re: ORDER BY with special order scheme?

strawberry wrote:

> see FIELD


Thanks, this works. Now I have the problem that the elements which are not
in the FIELD array are showed first, e.g. select type from foo order by
field(type, 99, 1, 88)

0
2
99
99
99
1
1
88

Is it possible that all the elements which are not in the field array are
ordered at the end of the other rows?

--
Frank Buss, fb@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
Reply With Quote
  #4 (permalink)  
Old 07-27-2007
Captain Paralytic
 
Posts: n/a
Default Re: ORDER BY with special order scheme?

On 27 Jul, 08:41, Frank Buss <f...@frank-buss.de> wrote:
> strawberry wrote:
> > see FIELD

>
> Thanks, this works. Now I have the problem that the elements which are not
> in the FIELD array are showed first, e.g. select type from foo order by
> field(type, 99, 1, 88)
>
> 0
> 2
> 99
> 99
> 99
> 1
> 1
> 88
>
> Is it possible that all the elements which are not in the field array are
> ordered at the end of the other rows?
>
> --
> Frank Buss, f...@frank-buss.dehttp://www.frank-buss.de,http://www.it4-systems.de


There are a couple of ways I would do this. Before I choose which one
to advise, a couple of questions:
How big is the field array likely to be?
Are the contents of the field array likely to change often?

Reply With Quote
  #5 (permalink)  
Old 07-27-2007
Frank Buss
 
Posts: n/a
Default Re: ORDER BY with special order scheme?

Captain Paralytic wrote:

> There are a couple of ways I would do this. Before I choose which one
> to advise, a couple of questions:
> How big is the field array likely to be?


It is small, maybe 5 entries.

> Are the contents of the field array likely to change often?


No.

--
Frank Buss, fb@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
Reply With Quote
  #6 (permalink)  
Old 07-27-2007
Captain Paralytic
 
Posts: n/a
Default Re: ORDER BY with special order scheme?

On 27 Jul, 09:53, Frank Buss <f...@frank-buss.de> wrote:
> Captain Paralytic wrote:
> > There are a couple of ways I would do this. Before I choose which one
> > to advise, a couple of questions:
> > How big is the field array likely to be?

>
> It is small, maybe 5 entries.
>
> > Are the contents of the field array likely to change often?

>
> No.
>
> --
> Frank Buss, f...@frank-buss.dehttp://www.frank-buss.de,http://www.it4-systems.de


I was about to advise you to use a case statement, but then I realised
that it is even easier than that.

The FIELD() function returns 0 if the string is not found. So just do
this:

SELECT
`type`
FROM `foo`
ORDER BY FIELD(type, 88, 1, 99) DESC


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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 04:58 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0