
- 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
Simulating MySQL's ORDER BY FIELD() in PostgreSQL?
The following is the process to simulate MySQL’s ORDER BY FIELD() in PostgreSQL.
We have used an Online Compiler to run PostgreSQL.
Let us now see what we did above to get the output.
Firstly, we created a table.
create table PostgreOrderIdDemo ( countryName varchar(20) );
Inserted records with the help of INSERT command.
insert into PostgreOrderIdDemo values('IND'); insert into PostgreOrderIdDemo values('US'); insert into PostgreOrderIdDemo values('UK');
The following is the syntax in PostgreSQL.
SELECT * FROM yourTableName ORDER BY CASE WHEN columnName='IND' THEN 1 WHEN columnName='US' THEN 2 WHEN columnName='UK' THEN 3 ELSE 0 END,columnName;
Example
Let us now use the above syntax to get the output.
SELECT * FROM PostgreOrderIdDemo ORDER BY CASE WHEN countryName='IND' THEN 1 WHEN countryName='US' THEN 2 WHEN countryName='UK' THEN 3 ELSE 0 END,countryName;
Output
The following is the output.
- Related Articles
- ORDER BY specific field value first in MySQL
- MySQL ORDER BY with custom field value
- MySQL order by field using CASE Statement
- MySQL ORDER BY Date field not in date format?
- How to use ORDER BY field and sort by id in a single MySQL field?
- MySQL IF/WHEN/ELSE/OR with ORDER BY FIELD
- How to ORDER BY FIELD with GROUP BY in a single MySQL query?
- Order by a single field and display rest of the records in the same order with MySQL
- Difference Between MySQL and PostgreSQL
- Order By date ASC in MySQL?
- Order by selected record in MySQL?
- MySQL query to perform sort order on same field
- Resolve Syntax error near “ORDER BY order DESC” in MySQL?
- MySQL Order by with case?
- MySQL Order By specific strings?

Advertisements