
- 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 records by first fixing the first two values in a column and then using DISTINCT to display other values in MySQL
Let us first create a table −
mysql> create table DemoTable ( FirstName varchar(100) ); Query OK, 0 rows affected (0.96 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Mike'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+ | FirstName | +-----------+ | Chris | | Robert | | Mike | | David | +-----------+ 4 rows in set (0.00 sec)
Following is the query wherein we are fixing the first two values with SELECT and then displaying rest of the values −
mysql> select 'Robert' AS FirstName UNION select 'David' AS FirstName UNION select DISTINCT(FirstName) AS FirstName from DemoTable;
This will produce the following output −
+-----------+ | FirstName | +-----------+ | Robert | | David | | Chris | | Mike | +-----------+ 4 rows in set (0.00 sec)
- Related Articles
- Display first non-null values with coalesce() in MySQL?
- How to ORDER BY DESC and display the first 3 records in MySQL?
- MySQL GROUP BY and CONCAT() to display distinct first and last name
- Query non-empty values of a row first in ascending order and then display NULL values
- Select distinct values from three columns and display in a single column with MySQL
- MySQL query to count occurrences of distinct values and display the result in a new column?
- Display and concatenate records ignoring NULL values in MySQL
- MySQL query to display record with maximum count values in a group with other column values?
- Display distinct dates in MySQL from a column with date records
- MySQL order by 0 first and then display the record in descending order?
- GROUP BY and display only non-empty column values in MySQL
- Multiply values of two columns and display it a new column in MySQL?
- MySQL query to display the count of distinct records from a column with duplicate records
- Find duplicate column values in MySQL and display them
- MySQL query to group by column and display the sum of similar values in another column

Advertisements