Jump to content

What are all of my options when ordering results in a database?


jdock1

Recommended Posts

I have an offer database that I want to order by a few different factors. I would rather order them from top to bottom, top being the most recent added value in the table. I was just wondering what are all of the ordering options? I know you can order by certian values in the table either desc or asc, but when I order from desc by the value in my table "date" its never right. I was kind of hoping that there is a built in function that orders them from most recently added? I would also like to have the option for a user to sort them through the credit value, which is a number from 1-5, with the highest to the lowest descending. It never seems to work when I order them from the number value, its always just random. I'm confused and if anybody can give me some insight I would appreciate it!

 

BTW, Im not a noob I just have never really worked with ordering SQL!

Link to comment
Share on other sites

After reading through the manual I still just dont get it. I dont understand WHAT its ordering by when I order it by a certain value. For example when I "ORDER BY date ASC" what is it going by? It just orders it in some damn random fashion? Its so confusing I just want to be able to order from the most recent added. I dont get ordering because when you order by a value it just orders it randomly, like if I wanted to sort it through highest value to lowest how the hell would I word that? Would I have to do something like this:

"ORDER BY pointsvalue WHERE pointsvalue = 5 DESC"

 

where 5 is the highest value. Sorry if Im being vague and making no sense I just am frustrated this makes no sense to me its so damn hard to order something!

Link to comment
Share on other sites

Okay, we'll need to know a little bit more about your database, specifically the data types of the columns, of the table(s) in question.

 

If, when you order the 'date' field in ascending or descending fashion, it does not order the way you think it should, it may be because of the data type of the column. For instance, a VARCHAR column is treated much differently than a date column; consequently, the data in those columns are treated differently as well.

 

Without knowing much more about your table, I would suggest converting your date columns to TIMPESTAMP columns. It makes ordering and mathematical date operations so much simpler because everything is just an integer. Then, you can order the TIMESTAMP column and the largest integer is the oldest and the smallest integer is the newest.

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.