Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example of using CASE WHEN in MySQL sorting

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

preface

In a previous project, the ability to use CASE WHEN sorting in SQL was used. Now write a blog memo ~

Database version: MySQL 5.6.42

Conditions:

A field indicates that the state of the data is a non-negative integer, and 0 indicates no state.

Requirements:

Sort in ascending order for this field, and you need to put data with a value of 0 last.

First let's look at the table structure:

The normal result of an ascending query is:

To put "Little Red" at the end of the list, write:

It can also be like this:

You can also use CASE WHEN to change the basis for sorting without affecting the query results:

summary

The above is the whole content of this article, I hope the content of this article has a certain reference value for everyone's study or work, if you have questions, you can leave a message to exchange, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report