PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > mysql.general > Sort results by order in list
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Sort results by order in list

Réponse
 
LinkBack Outils de la discussion
Vieux 30/10/2007, 04h53   #1
Papalagi Pakeha
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Sort results by order in list

Hello,

I have a query like:
SELECT id, start_date FROM iddt WHERE id IN ('109k7','s3x6','sxmns','wt57');
which gives me:
+-------+---------------------+
| id | start_date |
+-------+---------------------+
| 109k7 | 2007-10-07 12:06:58 |
| sxmns | 2007-10-06 02:17:30 |
| wt57 | 2007-10-07 15:57:37 |
| s3x6 | 2007-10-07 08:58:20 |
+-------+---------------------+

How can I get the results sorted by the order in which they appear in
the ID list? Indeed I could do it in the application but prefer to get
the results in the right order from MySQL as the ID list may be long
and I may need only first few entries cropped by LIMIT clause. Ideally
it should ORDER BY DATE(start_date) DESC and then by the ID list. Do I
need some sort of stored function for that?

I.e. the ideal output would be:
+-------+---------------------+
| id | start_date |
+-------+---------------------+
| 109k7 | 2007-10-07 12:06:58 |
| s3x6 | 2007-10-07 08:58:20 |
| wt57 | 2007-10-07 15:57:37 |
| sxmns | 2007-10-06 02:17:30 |
+-------+---------------------+

Thanks

PaPa
  Réponse avec citation
Vieux 30/10/2007, 06h54   #2
Peter Brawley
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sort results by order in list

>I.e. the ideal output would be:
>+-------+---------------------+
>| id | start_date |
>+-------+---------------------+
>| 109k7 | 2007-10-07 12:06:58 |
>| s3x6 | 2007-10-07 08:58:20 |
>| wt57 | 2007-10-07 15:57:37 |
>| sxmns | 2007-10-06 02:17:30 |
>+-------+---------------------+


What rule generates the order 109k7, s3x6, wt57, sxmns?

PB

Papalagi Pakeha wrote:
> Hello,
>
> I have a query like:
> SELECT id, start_date FROM iddt WHERE id IN ('109k7','s3x6','sxmns','wt57');
> which gives me:
> +-------+---------------------+
> | id | start_date |
> +-------+---------------------+
> | 109k7 | 2007-10-07 12:06:58 |
> | sxmns | 2007-10-06 02:17:30 |
> | wt57 | 2007-10-07 15:57:37 |
> | s3x6 | 2007-10-07 08:58:20 |
> +-------+---------------------+
>
> How can I get the results sorted by the order in which they appear in
> the ID list? Indeed I could do it in the application but prefer to get
> the results in the right order from MySQL as the ID list may be long
> and I may need only first few entries cropped by LIMIT clause. Ideally
> it should ORDER BY DATE(start_date) DESC and then by the ID list. Do I
> need some sort of stored function for that?
>
> I.e. the ideal output would be:
> +-------+---------------------+
> | id | start_date |
> +-------+---------------------+
> | 109k7 | 2007-10-07 12:06:58 |
> | s3x6 | 2007-10-07 08:58:20 |
> | wt57 | 2007-10-07 15:57:37 |
> | sxmns | 2007-10-06 02:17:30 |
> +-------+---------------------+
>
> Thanks
>
> PaPa
>
>

  Réponse avec citation
Vieux 30/10/2007, 09h12   #3
Papalagi Pakeha
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sort results by order in list

Hi,

It comes from external source together with Relevance value (float),
where the first ID has highest relevance and subsequent IDs are in
decreasing order.

For example:
109k7 1.79
s3x6 1.34
sxmns 1.21
wt57 0.93

I could use these numbers in the query as well if it s.

PaPa

On 10/30/07, Peter Brawley <peter.brawley@earthlink.net> wrote:
> >I.e. the ideal output would be:
> >+-------+---------------------+
> >| id | start_date |
> >+-------+---------------------+
> >| 109k7 | 2007-10-07 12:06:58 |
> >| s3x6 | 2007-10-07 08:58:20 |
> >| wt57 | 2007-10-07 15:57:37 |
> >| sxmns | 2007-10-06 02:17:30 |
> >+-------+---------------------+

>
> What rule generates the order 109k7, s3x6, wt57, sxmns?
>
> PB
>
> Papalagi Pakeha wrote:
> > Hello,
> >
> > I have a query like:
> > SELECT id, start_date FROM iddt WHERE id IN ('109k7','s3x6','sxmns','wt57');
> > which gives me:
> > +-------+---------------------+
> > | id | start_date |
> > +-------+---------------------+
> > | 109k7 | 2007-10-07 12:06:58 |
> > | sxmns | 2007-10-06 02:17:30 |
> > | wt57 | 2007-10-07 15:57:37 |
> > | s3x6 | 2007-10-07 08:58:20 |
> > +-------+---------------------+
> >
> > How can I get the results sorted by the order in which they appear in
> > the ID list? Indeed I could do it in the application but prefer to get
> > the results in the right order from MySQL as the ID list may be long
> > and I may need only first few entries cropped by LIMIT clause. Ideally
> > it should ORDER BY DATE(start_date) DESC and then by the ID list. Do I
> > need some sort of stored function for that?
> >
> > I.e. the ideal output would be:
> > +-------+---------------------+
> > | id | start_date |
> > +-------+---------------------+
> > | 109k7 | 2007-10-07 12:06:58 |
> > | s3x6 | 2007-10-07 08:58:20 |
> > | wt57 | 2007-10-07 15:57:37 |
> > | sxmns | 2007-10-06 02:17:30 |
> > +-------+---------------------+
> >
> > Thanks
> >
> > PaPa
> >
> >

>

  Réponse avec citation
Vieux 30/10/2007, 10h38   #4
Sebastian Mendel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sort results by order in list

Papalagi Pakeha schrieb:
> Hello,
>
> I have a query like:
> SELECT id, start_date FROM iddt WHERE id IN ('109k7','s3x6','sxmns','wt57');
> which gives me:
> +-------+---------------------+
> | id | start_date |
> +-------+---------------------+
> | 109k7 | 2007-10-07 12:06:58 |
> | sxmns | 2007-10-06 02:17:30 |
> | wt57 | 2007-10-07 15:57:37 |
> | s3x6 | 2007-10-07 08:58:20 |
> +-------+---------------------+
>
> How can I get the results sorted by the order in which they appear in
> the ID list?


i do not fully understand ... ORDER BY `id` ... ???
what ID List?

> Indeed I could do it in the application but prefer to get
> the results in the right order from MySQL as the ID list may be long
> and I may need only first few entries cropped by LIMIT clause. Ideally
> it should ORDER BY DATE(start_date) DESC and then by the ID list. Do I
> need some sort of stored function for that?


ORDER BY DATE(`start_date`) DESC, `id`


> I.e. the ideal output would be:
> +-------+---------------------+
> | id | start_date |
> +-------+---------------------+
> | 109k7 | 2007-10-07 12:06:58 |
> | s3x6 | 2007-10-07 08:58:20 |
> | wt57 | 2007-10-07 15:57:37 |
> | sxmns | 2007-10-06 02:17:30 |
> +-------+---------------------+



  Réponse avec citation
Vieux 30/10/2007, 10h40   #5
Papalagi Pakeha
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sort results by order in list

On 10/30/07, Sebastian Mendel <lists@sebastianmendel.de> wrote:
> Papalagi Pakeha schrieb:
> > Hello,
> >
> > I have a query like:
> > SELECT id, start_date FROM iddt WHERE id IN ('109k7','s3x6','sxmns','wt57');
> > which gives me:
> > +-------+---------------------+
> > | id | start_date |
> > +-------+---------------------+
> > | 109k7 | 2007-10-07 12:06:58 |
> > | sxmns | 2007-10-06 02:17:30 |
> > | wt57 | 2007-10-07 15:57:37 |
> > | s3x6 | 2007-10-07 08:58:20 |
> > +-------+---------------------+
> >
> > How can I get the results sorted by the order in which they appear in
> > the ID list?

>
> i do not fully understand ... ORDER BY `id` ... ???
> what ID List?


This ID list: ('109k7','s3x6','sxmns','wt57')
I.e. I want to get the results in the same order in which they appear
in the above list. 109k7 first, s3x6 second, etc. Simple WHERE id IN
(...) gives me random order.

PaPa
  Réponse avec citation
Vieux 30/10/2007, 11h15   #6
Johan Höök
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sort results by order in list

Hi,
what you probably want is
SELECT id, start_date FROM iddt WHERE id IN
('109k7','s3x6','sxmns','wt57')
ORDER BY FIELD(id,'109k7','s3x6','sxmns','wt57')

/Johan

Papalagi Pakeha skrev:
> On 10/30/07, Sebastian Mendel <lists@sebastianmendel.de> wrote:
>> Papalagi Pakeha schrieb:
>>> Hello,
>>>
>>> I have a query like:
>>> SELECT id, start_date FROM iddt WHERE id IN ('109k7','s3x6','sxmns','wt57');
>>> which gives me:
>>> +-------+---------------------+
>>> | id | start_date |
>>> +-------+---------------------+
>>> | 109k7 | 2007-10-07 12:06:58 |
>>> | sxmns | 2007-10-06 02:17:30 |
>>> | wt57 | 2007-10-07 15:57:37 |
>>> | s3x6 | 2007-10-07 08:58:20 |
>>> +-------+---------------------+
>>>
>>> How can I get the results sorted by the order in which they appear in
>>> the ID list?

>> i do not fully understand ... ORDER BY `id` ... ???
>> what ID List?

>
> This ID list: ('109k7','s3x6','sxmns','wt57')
> I.e. I want to get the results in the same order in which they appear
> in the above list. 109k7 first, s3x6 second, etc. Simple WHERE id IN
> (...) gives me random order.
>
> PaPa
>

  Réponse avec citation
Vieux 30/10/2007, 12h35   #7
Papalagi Pakeha
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sort results by order in list

On 10/30/07, Johan Höök <johan.hook@pythagoras.se> wrote:
> Hi,
> what you probably want is
> SELECT id, start_date FROM iddt WHERE id IN
> ('109k7','s3x6','sxmns','wt57')
> ORDER BY FIELD(id,'109k7','s3x6','sxmns','wt57')


That's exactly it! Thanks a lot :-)

Just for the record, here's the FIELD() function description:
http://dev.mysql.com/doc/refman/5.0/...function_field

PaPa
  Réponse avec citation
Vieux 30/10/2007, 15h51   #8
Peter Brawley
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sort results by order in list

PaPa,

>It comes from external source together with Relevance value (float),


Then you need to ORDER BY that func.

PB

-----

Papalagi Pakeha wrote:
> Hi,
>
> It comes from external source together with Relevance value (float),
> where the first ID has highest relevance and subsequent IDs are in
> decreasing order.
>
> For example:
> 109k7 1.79
> s3x6 1.34
> sxmns 1.21
> wt57 0.93
>
> I could use these numbers in the query as well if it s.
>
> PaPa
>
> On 10/30/07, Peter Brawley <peter.brawley@earthlink.net> wrote:
>
>>> I.e. the ideal output would be:
>>> +-------+---------------------+
>>> | id | start_date |
>>> +-------+---------------------+
>>> | 109k7 | 2007-10-07 12:06:58 |
>>> | s3x6 | 2007-10-07 08:58:20 |
>>> | wt57 | 2007-10-07 15:57:37 |
>>> | sxmns | 2007-10-06 02:17:30 |
>>> +-------+---------------------+
>>>

>> What rule generates the order 109k7, s3x6, wt57, sxmns?
>>
>> PB
>>
>> Papalagi Pakeha wrote:
>>
>>> Hello,
>>>
>>> I have a query like:
>>> SELECT id, start_date FROM iddt WHERE id IN ('109k7','s3x6','sxmns','wt57');
>>> which gives me:
>>> +-------+---------------------+
>>> | id | start_date |
>>> +-------+---------------------+
>>> | 109k7 | 2007-10-07 12:06:58 |
>>> | sxmns | 2007-10-06 02:17:30 |
>>> | wt57 | 2007-10-07 15:57:37 |
>>> | s3x6 | 2007-10-07 08:58:20 |
>>> +-------+---------------------+
>>>
>>> How can I get the results sorted by the order in which they appear in
>>> the ID list? Indeed I could do it in the application but prefer to get
>>> the results in the right order from MySQL as the ID list may be long
>>> and I may need only first few entries cropped by LIMIT clause. Ideally
>>> it should ORDER BY DATE(start_date) DESC and then by the ID list. Do I
>>> need some sort of stored function for that?
>>>
>>> I.e. the ideal output would be:
>>> +-------+---------------------+
>>> | id | start_date |
>>> +-------+---------------------+
>>> | 109k7 | 2007-10-07 12:06:58 |
>>> | s3x6 | 2007-10-07 08:58:20 |
>>> | wt57 | 2007-10-07 15:57:37 |
>>> | sxmns | 2007-10-06 02:17:30 |
>>> +-------+---------------------+
>>>
>>> Thanks
>>>
>>> PaPa
>>>
>>>
>>>

>
>
>


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h16.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,21412 seconds with 16 queries