Forum Moderators: open

Message Too Old, No Replies

MySql - ORDER BY specific value as first result

which is the right function to get this...

         

guajiro

1:03 pm on Sep 29, 2007 (gmt 0)

10+ Year Member



Hi guys,

I have a very simple MySql query. I just want to have the results ORDERED with specific filed value at first result, this value will be passed by URL.

SELECT *
FROM photos
ORDER BY?

I tried the following way and it worked only when I enter direct value, not when I pass the url variable:

SELECT *
FROM photos
ORDER BY FIND_IN_SET(2,photoID ) DESC

--> here I get at first place where photoID is 2

This do not work:

SELECT *
FROM photos
ORDER BY FIND_IN_SET(colname,photoID ) DESC

(colname is $_GET['x'])

Is FIND_IN_SET the right function to do this, or there is another one?

Guys help me out, I've been trying this all saturday... no way to have it working.

Thanks in advance
:-)

guajiro

9:03 pm on Sep 29, 2007 (gmt 0)

10+ Year Member



Can someone help me out with this please!...

I explain it again to make sure you guys understand:

I have a simple Recordset in Dreamweaver, the query is:

SELECT *
FROM photos

The point is that I want some specific ORDER here. I want the first record on the result to be and specific value, eg. where photoID is = 5. I pass this number by URL parameter.

I want this result:


photoID, name
--------------
5 Hawaii
2 Habana
4 Miami
6 Japan

Waiting here for some help...

Thanks again!

syber

12:04 am on Sep 30, 2007 (gmt 0)

10+ Year Member



You should be able to use the CASE statement here.


SELECT *, CASE photoID
WHEN 5 THEN 1
WHEN 2 THEN 2
WHEN 4 THEN 3
WHEN 6 THEN 4
ELSE photoID
END AS sortkey
FROM photos
ORDER BY sortkey DESC

guajiro

9:12 am on Sep 30, 2007 (gmt 0)

10+ Year Member



Hi,

Thanks for your reply.

I've sorted the problem using ORDER BY FIELD:

SELECT *
FROM photos
ORDER BY FIELD(photoID, colname) DESC

Work GREAT!