Bluehost.com Web Hosting $6.95

Select Records In Order Based on an Ordered ID List

This is a discussion on Select Records In Order Based on an Ordered ID List within the MySQL Database forums, part of the Database Forums category; Hello! Let's say I have an ordered list of ids that correspond to an ordered list of records I ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-03-2007
Mike
 
Posts: n/a
Default Select Records In Order Based on an Ordered ID List

Hello!

Let's say I have an ordered list of ids that correspond to an ordered
list of records I want to retrieve, ie. 32,80,10,90,28,5

What's the most efficient way to retrieve the records corresponding to
the ids in the same order as the ids? It seems the IN clause doesn't
preserve ordering, and using UNION to submit the same query with
different id WHERE clauses seems messy/inefficient.

Thanks!
Mike

Reply With Quote
  #2 (permalink)  
Old 03-03-2007
Gordon Burditt
 
Posts: n/a
Default Re: Select Records In Order Based on an Ordered ID List

>Let's say I have an ordered list of ids that correspond to an ordered
>list of records I want to retrieve, ie. 32,80,10,90,28,5
>
>What's the most efficient way to retrieve the records corresponding to
>the ids in the same order as the ids? It seems the IN clause doesn't
>preserve ordering,


There's no ordering in an IN clause to preserve. If you don't ORDER BY,
you have no complaint about the order you get.

>and using UNION to submit the same query with
>different id WHERE clauses seems messy/inefficient.


Create a table ordering with columns consisting of the ids and the
relative ordering.
id ordering
32 1
80 2
10 3
90 4
28 5
5 6

Join the table against the table with the
records. ORDER BY ordering.ordering desc .

This may not be most efficient, but it shouldn't be too bad.

Reply With Quote
  #3 (permalink)  
Old 03-03-2007
Jerry Stuckle
 
Posts: n/a
Default Re: Select Records In Order Based on an Ordered ID List

Mike wrote:
> Hello!
>
> Let's say I have an ordered list of ids that correspond to an ordered
> list of records I want to retrieve, ie. 32,80,10,90,28,5
>
> What's the most efficient way to retrieve the records corresponding to
> the ids in the same order as the ids? It seems the IN clause doesn't
> preserve ordering, and using UNION to submit the same query with
> different id WHERE clauses seems messy/inefficient.
>
> Thanks!
> Mike
>


SELECT myval FROM mytable
WHERE id IN (32,80,10,90,28,5)
ORDER BY FIND_IN_SET(id, '32,80,10,90,28,5')

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
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:47 AM.


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