Jump to content

How do I do a sort within a sort


phpjayx

Recommended Posts

I have one sort working correctly....

 

$sql_select = "select * from data where userid='" . $param['userid'] . "' order by timestamp asc";

 

 

But if I also have a number ($param['number2']), which I also want to be included in the sort, what is the best way to do this?

 

Thanks in advance

Link to comment
Share on other sites

But why would you add a number to a sort? MySQL will interpret it as sort by the nth column (afaik, not sure if it does that if you've named another column in your order). If you're trying to do that, just use the name of the column. 

Link to comment
Share on other sites

So you think someone who doesn't know to simply add the number after his existing sort, actually knows that MySQL allows you to use the column numbers and wants to sort on it?

*shrug* Barand you have more faith in people than I do I guess.

Link to comment
Share on other sites

Well you are correct that I know just the basics of the capabilities of MySQL, still learning....

 

Basically for this sort. I have grade numbers for people, that is my (number2), then the timestamp which is a checkin time,

I was hoping to group all of the people with the same grade numbers together, but as well sort in ascending order

Link to comment
Share on other sites

Then you'll need to sort it on grade number first, and timestamp second. If you follow the link I posted above, you'll see how to sort on multiple fields.

 

That said, I'm still not quite sure where the $param['number2'] variable comes into play here. In the best case scenario it doesn't, and you just included it in your description out of your own confusion. The worst case scenario would mean that you need to normalize your database tables, if you have multiple fields to signify the grade numbers.

So, please post a bit more detail on what that variable contains, how you currently use it, and the table structure for the table in question. Remember, use accurate examples that showcase exactly what have; The more accurate information you give us, the easier it'll be for us to help you. ;)

Link to comment
Share on other sites

You're most welcome, glad I could help. :)

Also, thanks for posting the solution you reached. I'm sure others will find it useful.

 

PS: You might need to look up on output escaping, and input validation, for your query. Not sure where the $param array comes from, but you might be open for SQL injections there. Since you're including a user ID, I'm going to assume it's an integer value, which means that typecasting it to an int before adding it to the query should be sufficient.

Link to comment
Share on other sites

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.