It seems you are confused about what ORDER BY is for. You order by a column or set of columns. I think your intent is to get the results with a custom order of 987, 321, 654. In order to do this, you have to do a bit of trickery. Fortunately when SQL builds its result set, it processes the order by after building the columns, so you can create some custom column results then order by that. In this case, we can use to the SQL CASE statement to create your "intSortBy" column. This should work:
SELECT *, CASE idCompany WHEN 987 THEN 1 WHEN 321 THEN 2 WHEN 654 THEN 3 END AS Order
FROM tCompanies
WHERE idCompany IN (987, 321, 654)
ORDER BY Order
Now, 987 will be 1st, 321 2nd, and 654 3rd. Add columns to the ORDER BY clause as needed, and add cases to the CASE as needed.
|