Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ordering by value list? 1

Status
Not open for further replies.

OsakaWebbie

Programmer
Feb 11, 2003
628
JP
I want to use a list of values for a field as the basis for a query (so far, so good - that's "WHERE col IN (comma-delimited list)"), but I want to get the results in the same order as the list of values. Is there a way?
 
You could use:
[tt]
ORDER BY FIND_IN_SET(fld1,'dog,cat,cow,rat,pig')
[/tt]
or:
[tt]
ORDER BY FIELD(fld1,'dog','cat','cow','rat','pig')
[/tt]
 
Wow, that was a quick response! I just looked those functions up in the MySQL docs and they are both listed as string functions. In my case, though, the field is numeric rather than string (the auto-incremented primary index of a table) - can I still use one of those methods?
 
Sure. Numeric values will be converted to strings before comparison.
 
Thanks! I haven't had time to implement it yet, but I'm sure it will be fine. Before posting I tried to search for the answer myself, but "where in order by" isn't very specific, and those are the only relevent words I knew. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top