Jump to content

SELECT ... ORDER BY an array....


briant

Recommended Posts

I'm new when it comes to arrays. It's pretty confusing but I will try my best to describe what I am trying to accomplish.

 

I would like to SELECT fields from a database and order it. Here is an example.

 

SELECT * FROM mytable ORDER BY ranks;

 

The ranks field has: 11,5,33,2,66,39

So if you were to explode it into an array, the [0] value would be 11.

I want my results to be ordered by the [0] array. How am I to do that?

 

I suppose if you were to nest it, it could work but I'm pretty sure there's an easier way.

 

Thanks for reading... would appreciate any help.

Link to comment
Share on other sites

If I'm able to explode it and call that [ 0 ] value all in one line, I think, then I'll be able to order it.

 

SELECT * FROM mytable ORDER BY explode(",", ranks); // But this is not correct.

 

For some reason, I think I should also add the 'list' syntax like I see in this example:

 

$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user; // foo
echo $pass; // *

Link to comment
Share on other sites

Ok, thats probably not the best way to store the ranks, hence making it more difficult to sort by... anyway. Something like....

 

SELECT *, SUBSTR(ranks,LOCATE(',',ranks)-1) AS firstranks FROM mytable ORDER BY firstranks;

 

May help. Not at all tested though.

Link to comment
Share on other sites

  • 1 year later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.