
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to order and select query with conditions in MySQL?
Following is the syntax −
select * from yourTableName order by yourColumnName=0,yourColumnName;
Let us first create a table −
mysql> create table DemoTable1348 -> ( -> Amount int -> ); Query OK, 0 rows affected (0.80 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1348 values(100); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1348 values(0); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1348 values(90); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1348 values(45); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1348 values(0); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1348 values(89); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1348;
This will produce the following output −
+--------+ | Amount | +--------+ | 100 | | 0 | | 90 | | 45 | | 0 | | 89 | +--------+ 6 rows in set (0.00 sec)
Following is the query to order and select query with conditions −
mysql> select * from DemoTable1348 order by Amount=0,Amount;
This will produce the following output −
+--------+ | Amount | +--------+ | 45 | | 89 | | 90 | | 100 | | 0 | | 0 | +--------+ 6 rows in set (0.00 sec)
- Related Articles
- MySQL query with OR & AND conditions
- MySQL query to select a count on two separate conditions?
- MySQL query to select distinct order by id
- How to order results of a query randomly & select random rows in MySQL?
- MySQL query to select rows except first row in descending order?
- Select with set order in MySQL
- How can you order the result obtained by select query in MySQL?
- Select query with regular expression in MySQL
- Insert with a Select query in MySQL
- MySQL select query with multiple WHERE?
- How to do recursive SELECT query in MySQL?
- How to use alias in MySQL select query?
- How to use prepared statement for select query in Java with MySQL?
- MySQL query to select records with a particular date and time?
- MySQL SELECT query to return records with specific month and year

Advertisements