
- 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
Display first selected row in MySQL?
You can use ORDER BY for this. Let us first create a table −
mysql> create table DemoTable ( Value int ); Query OK, 0 rows affected (0.20 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable values(8); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(18); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable values(11); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(9); Query OK, 1 row affected (0.05 sec) mysql> insert into DemoTable values(19); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable values(6); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(7); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+ | Value | +-------+ | 20 | | 8 | | 18 | | 11 | | 9 | | 19 | | 6 | | 7 | +-------+ 8 rows in set (0.00 sec)
Following is the query to display first selected row in MySQL −
mysql> select *from DemoTable order by (Value=9) DESC,Value DESC;
This will produce the following output −
+-------+ | Value | +-------+ | 9 | | 20 | | 19 | | 18 | | 11 | | 8 | | 7 | | 6 | +-------+ 8 rows in set (0.00 sec)
- Related Articles
- MySQL limit in a range fail to display first 3 row?
- Display multiple selected rows using MySQL IN()
- Display selected records from a MySQL table with IN() operator
- Only display row with highest ID in MySQL
- Implement MySQL IN for 2 columns to display only selected records
- Display random row from a MySQL table
- Display sum in last row of table using MySQL?
- Display first non-null values with coalesce() in MySQL?
- Get the first 10 rows followed by the syntax to display remaining row records with a single MySQL query
- MySQL query to select rows except first row in descending order?
- How to select first and last row record using LIMIT in MySQL?
- Display records by first fixing the first two values in a column and then using DISTINCT to display other values in MySQL
- Query non-empty values of a row first in ascending order and then display NULL values
- Display all the column values in a single row separated by comma in MySQL?
- Order by selected record in MySQL?

Advertisements