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!

How to get the right order in an WHERE xx IN query?

Status
Not open for further replies.

glubschi

IS-IT--Management
Aug 2, 2005
2
DE
Hi all,

I´ve the following problem:

2 tables. The first (table1) has a column id and the second some names.
Now I have the second table with a field that has e.g. the following value:
"6 3 9"
(id=6 id=3 id=9)

My query:
SELECT id, names from table1 WHERE id IN (6, 3, 9)

The result looks like this:
id names
3 blabla
6 XXXXX
9 galalala

But I want to have the result in the right order like this:
id names
6 XXXXX
3 blabla
9 galalala

How can I get this?

I could do 3 queries like "SELECT id, names from tableX WHERE id=6" + "SELECT id, names from tableX WHERE id=3" but this not what i want.

Thx for your help
 
You could use:
[tt]
SELECT id, names
FROM tableX
WHERE id IN (6,3,9)
ORDER BY FIELD(id,6,3,9)
[/tt]
 
Many thx Tony. That was exactly the query that I´m looking for.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top