MYSQL order by both Ascending and Descending sorting

150,538

You can do that in this way:

ORDER BY `products`.`product_category_id` DESC ,`naam` ASC

Have a look at ORDER BY Optimization

Share:
150,538
Tschallacka
Author by

Tschallacka

In the summer of '99 I started to become more interested in the programs that made the computers run and my father bought me a couple of books on programming and I started reading and experimenting with it. The hardest part was understanding how variables worked. This was in the time before you could go to the internet to answer any question because google didn't exist yet, and I didn't know where to look. I remeber vividly tossing the Java step by step book frustrated in a corner because it was that little understanding that hindered me from understanding the rest. It lay there for 4-6 months until suddenly, when I had all but forgotten about it it clicked in my head and I understood how variables worked. I got the book up and started reading, and suddenly I could understand how it worked, how it translated and a world opened for me. The year that followed I learned, Java, Perl, HTML, JavaScript, C++, Visual Basic, PHP and possibly a few other programming languages I have forgotten about. It was the basis for my understanding the digital world. A couple of projects I remember making are a forum, a remake of pokemon in javascript with map creator, a room walk through like you happen to see on a lot of house buying websites, a direct x 7 game in C++, a book record keeper in java to keep track of the books I had. From there I kept doing a couple of hobby projects off and on, started several relatively successful fora and stopped them when the trolls found them and turned the entire place toxic and stressful, had some websites for fun where I would gather jokes, that site had a lot of CSS things in it I had experimented with, another was to gather and display poems. I discontinued them at some point because I didn't wish to pay for the server costs anymore for sites that I rarely used anymore and didn't have that many users/visitors. I was able to get a job at a company as a programmer and there my skills deepened by having more contact with other programmers and learning about other possibilities and how to cooperate with them, take the lead in some aspects, and follow the lead in others. After that job I went on to another job where even more responsibility was put on my shoulder by transforming a static html site that was updated by frontpage into a dynamic responsive website, without a team to fall back on. This was a pretty challenging thing because I had to combine a lot of varied items into one coherent thing. During these jobs I also started coding a Minecraft Mod, Magic Cookies and had great fun exploring how to expand Minecraft functionality with my ideas. Basically the entire mod is a test bed of me trying to see what is possible within Minecraft code. I had great fun figuring out how large structures are rendered like nether temples and recreated my own with my dark temple that spawns in the nether, the same way a nether structure does, without having to rely on chunks being loaded like normal spawning of buildings do.

Updated on February 16, 2020

Comments

  • Tschallacka
    Tschallacka about 4 years

    I have a mysql table with products.

    The products have a category ID and a name.

    What I'd like to do is order by category id first descending order and then order by product name ascending order.

    SELECT * FROM `products` ORDER BY `products`.`product_category_id`,`naam` DESC
    

    What i'd like is

    SELECT * FROM `products` ORDER BY `products`.`product_category_id`,`naam` DESC,ASC
    

    but that unfortunately doesn't work.

    Is this even possible in mysql to define the sorting order of the second sorting column?

  • Tschallacka
    Tschallacka over 11 years
    Then me wonders why this is so hard to find on google... probaly looking with the wrong keywords. I will accept your answer in a few minute.
  • Himanshu
    Himanshu over 11 years
  • Tschallacka
    Tschallacka over 11 years
    Yea, it gives the order by, been there, but hardly any of the manuals of tutorials highlight how to do two order by's.
  • Himanshu
    Himanshu over 11 years
    @MichaelDibbets: Did you see this manual?
  • Tschallacka
    Tschallacka over 11 years
    Now that you have pointed it out to me. All I got when searching for order by's were just normal single column order by instructions or how to order multiple columns by the same as/descending order. I really must thank you for your answer.