- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to place number 0 from a column at the end maintaining the ascending search order in MySQL?
Let us first create a table −
mysql> create table DemoTable -> ( -> Number int -> ); Query OK, 0 rows affected (0.58 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(9); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(4); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable values(0); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(7); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(11); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable
Output
This will produce the following output −
+--------+ | Number | +--------+ | 9 | | 4 | | 0 | | 7 | | 11 | +--------+ 5 rows in set (0.00 sec)
Following is the query to use ORDER BY and place 0's at the end maintaining the ascending search order −
mysql> select *from DemoTable order by Number=0,Number;
Output
This will produce the following output −
+--------+ | Number | +--------+ | 4 | | 7 | | 9 | | 11 | | 0 | +--------+ 5 rows in set (0.00 sec)
- Related Articles
- How to order records by a column in MySQL and place empty records at the end?
- How to Order by date in MySQL but place empty dates in the end?
- MySQL Order by a specific column x and display remaining values in ascending order
- Maintaining order in MySQL “IN” query?
- How to search on a MySQL varchar column using a number?
- MySQL command to order timestamp values in ascending order?
- How to sort a particular value at the end in MySQL?
- How to select the last three rows of a table in ascending order with MySQL?
- How to arrange the fractions in ascending order and descending order?
- Sort a list in MySQL and display a fixed result at the end of the column?
- How can we sort MySQL output in ascending order?
- MySQL query to display ASC order in number column?
- How can I insert a value in a column at the place of NULL using MySQL COALESCE() function?
- How to sort a column of data.table object in ascending order using column name stored in a vector in R?
- MySQL command to order timestamp values in ascending order without using TIMESTAMP()?

Advertisements