
- 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
Delete all rows except only a specific row in MySQL?
To delete all rows except a single specific row, try the below syntax −
delete from yourTableName where yourColumnName!=yourValue;
Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Subject varchar(100) ); Query OK, 0 rows affected (0.65 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Subject) values('MySQL'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(Subject) values('MongoDB'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(Subject) values('Java'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+----+---------+ | Id | Subject | +----+---------+ | 1 | MySQL | | 2 | MongoDB | | 3 | Java | +----+---------+ 3 rows in set (0.00 sec)
Here is the query to delete all rows except a single specific ow −
mysql> delete from DemoTable where Subject!='MongoDB'; Query OK, 2 rows affected (0.19 sec)
Let us check all records from the table once again −
mysql> select *from DemoTable;
Output
+----+---------+ | Id | Subject | +----+---------+ | 2 | MongoDB | +----+---------+ 1 row in set (0.00 sec)
- Related Articles
- Delete only specific rows in a table with MySQL
- How to delete all rows except some in MySQL?
- Select all rows except from today in MySQL?
- MySQL query to select rows except first row in descending order?
- MySQL query to get a specific row from rows
- Get only a single value from a specific MySQL row?
- Return only a single row from duplicate rows with MySQL
- Delete only some rows from a table based on a condition in MySQL
- How can we delete all rows from a MySQL table?
- How to select all rows from a table except the last one in MySQL?
- MySQL: delete all rows containing string “foo” in sample table “bar”?
- MySQL query to delete all rows older than 30 days?
- How to select all the records except a row with certain id from a MySQL table?
- MySQL query to delete row
- Python - Sum only specific rows of a Pandas Dataframe

Advertisements