views:

43

answers:

2

so here's the problem

I have 3 types for a hotel

  1. premium
  2. featured
  3. basic

I am trying to display a list of hotels and sort them so that featured hotels are the first shown

what mysql query would accomplish this?

EDIT here's the table, with some fields stripped

ID | hotel_name | type
=======================
1 | Aria Hotel | basic
+1  A: 
ORDER BY FIELD(type, 'featured', 'premium', 'basic')

You could rearrange 'featured', 'premium', 'basic' upto your needs, and may also put DESC there too

For example

ORDER BY FIELD(type, 'featured', 'premium', 'basic') DESC

or to arrange the rest as normal sorting order.

ORDER BY FIELD(type, 'featured', 'premium', 'basic') DESC, type;
S.Mark
I can only use this function http://docs.kohanaphp.com/libraries/database/builder#orderbyso, other than DESC and ASC, I have no other option
Ygam
Imm, so could you specify "with Kohana" in your question title then? and also tag with `kohana`
S.Mark
I guess, you will end up by calling `->query` directly instead of `->orderby` method like `$this->db->query("SELECT * FROM table ORDER BY FIELD(type, 'featured', 'premium', 'basic') DESC, type;");`
S.Mark
Nice. Is there a FIELD equivalent in MS SQL Server ?
Liao
@Ygam, the link you mention, has this code `$db->orderby(NULL, 'RAND()');` .. so, can you not use the FIELD() function that way?
Liao
the second parameter accepts only 3 strings, ASC, DESC, RAND(), RANDOM(), NULL as values. if anything other than that is specified, it defaults to ASC (I was reading code minutes ago)
Ygam
A: 

I will use group by on hotel type and sort by desc in your case. Try writing the query.

Algorist