DO YOU NEED A CONTENT WRITER FOR YOUR BUSINESS?

Your One-Stop Solution for All Content Needs! Click here for more!
MySQL

MySQL ORDER BY Clause

Share

Using MySQL ORDER BY Clause you can sort table columns by ascending or descending.

Suppose we have a table ‘tbl’ with column ‘name’ and a row like below values:

1
1B
10
2
10C
2A
10Z

So, when you sort by name:

SELECT name
FROM tbl
ORDER BY name;

So, when you sort by that column you will get the result:

1
10
10C
10Z
1B
2
2A

This is not like that what we expected. This ia natural sorting.
So we need to CAST this to get our desired result like below:

SELECT name
FROM tbl
ORDER BY CAST(name AS UNSIGNED);

Result:
1
1B
2
2A
10
10C
10Z

ORDER BY FIELD:

Sometimes a specific order is required in a query which cannot be done using either ASC or DESC or using a special sort field.
Using FIELD( ) function in the ORDER BY clause you can achieve this.

SELECT name
FROM tbl
ORDER BY FIELD(name, '1B', '2A') desc;

Result:
2A
1B
1
10
2
10C
10Z

Namaste UI (Author)

Namaste UI collaborates closely with clients to develop tailored guest posting strategies that align with their unique goals and target audiences. Their commitment to delivering high-quality, niche-specific content ensures that each guest post not only meets but exceeds the expectations of both clients and the hosting platforms. Connect with us on social media for the latest updates on guest posting trends, outreach strategies, and digital marketing tips. For any types of guest posting services, contact us on info[at]namasteui.com.

Recent Posts

How Small Brands Can Make Offline Touchpoints Feel More Memorable

Most small brands spend a lot of energy improving digital touchpoints. They refine their websites,…

7 minutes ago

What Should You Do When a Dog Bites You?

Not every dog is friendly, and even friendly dogs can attack when threatened or provoked.…

4 hours ago

Holding Manufacturers Accountable: Steps to File a Lawsuit for Pressure Cooker Explosions

Pressure cookers have become a favored kitchen appliance because they can quickly and efficiently cook…

1 day ago

Reasons to Hire a Shopify Web Designer for Your Business

Shopify has quickly become the preferred platform for businesses seeking to build an online presence.…

4 days ago

How To Gain Free Instagram Followers and Likes Easily-GetInsta

We all know that Instagram is a very famous social platform that attracts more and…

7 days ago

How to tell whether high-level glass is toughened or laminated

Glass at height is easy to overlook, in both senses. It is out of reach,…

2 weeks ago