
- 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
Remove specific fields/ rows and show other records in MySQL?
For this, use CASE WHEN statement in MySQL. Let us create a table −
mysql> create table demo47 −> ( −> first_name varchar(20), −> last_name varchar(20) −> ); Query OK, 0 rows affected (1.57 sec)
Insert some records into the table with the help of insert command −
mysql> insert into demo47 values('John','Smith'); Query OK, 1 row affected (0.18 sec) mysql> insert into demo47 values('David','Miller'); Query OK, 1 row affected (0.11 sec) mysql> insert into demo47 values('John','Doe'); Query OK, 1 row affected (0.12 sec) mysql> insert into demo47 values('Chris','Brown'); Query OK, 1 row affected (0.12 sec)
Display records from the table using select statement −
mysql> select *from demo47;
This will produce the following output −
+------------+-----------+ | first_name | last_name | +------------+-----------+ | John | Smith | | David | Miller | | John | Doe | | Chris | Brown | +------------+-----------+ 4 rows in set (0.00 sec)
Following is the query to remove specific fields/ rows and display other records in MySQL.
mysql> select case when first_name= 'John' then last_name else first_name end Result −> from demo47 −> where 'John' in (first_name,last_name);
This will produce the following output −
+--------+ | Result | +--------+ | Smith | | Doe | +--------+ 2 rows in set (0.00 sec)
- Related Articles
- Order records and delete n rows in MySQL
- How to ignore specific records and add remaining corresponding records (numbers) in MySQL?
- How select specific rows in MySQL?
- Remove first two characters of all fields in MySQL?
- Query in MySQL for string fields with a specific length?
- Select specific rows in a range with MySQL?
- Fetch ordered records after a specific limit in MySQL
- Fetch records containing a specific character twice in MySQL
- How to display a specific field in array using $project in MongoDB and ignore other fields?
- Remove new line characters from rows in MySQL?
- MySQL query to remove Null Records in a column?
- MySQL SELECT query to return records with specific month and year
- MySQL to fetch records based on a specific month and year?
- Select all records if it contains specific number in MySQL?
- Delete only specific rows in a table with MySQL

Advertisements