- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Exclude rows based on column value when another duplicate column value is found in MySQL?
For this, you can use subquery. Let us first create a −
mysql> create table DemoTable1427 -> ( -> StudentId int, -> StudentMarks int -> ); Query OK, 0 rows affected (1.28 sec)
Insert some records in the table using insert −
mysql> insert into DemoTable1427 values(201,89); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1427 values(201,99); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1427 values(210,98); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select −
mysql> select * from DemoTable1427 ;
This will produce the following output −
+-----------+--------------+ | StudentId | StudentMarks | +-----------+--------------+ | 201 | 89 | | 201 | 99 | | 210 | 98 | +-----------+--------------+ 3 rows in set (0.00 sec)
Here is the query to exclude rows based on column value when another duplicate column value is found. Here, Studentmarks 89 is with StudentId 201 and with that Studentid 201 is also having another record, therefore both will get excluded from the result −
mysql> select * from DemoTable1427 -> where StudentId NOT IN (select distinct StudentId from DemoTable1427 where StudentMarks=89);
This will produce the following output −
+-----------+--------------+ | StudentId | StudentMarks | +-----------+--------------+ | 210 | 98 | +-----------+--------------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL DATE_ADD() to increment a date based on the value in another column?
- MySQL query to select rows where column value is only 0, group by another column?
- Update a column based on another MySQL table’s column
- How to extract a data frame’s column value based on a column value of another data frame in R?
- How to assign a column value in a data frame based on another column in another R data frame?
- Get first date from timestamp in MySQL group by another column with duplicate value
- How to find the position of a data frame’s column value based on a column value of another data frame in R?
- Find rows that have the same value on a column in MySQL?
- Perform MySQL UPDATE on the basis of DATE value in another column
- Group MySQL rows in an array by column value?
- Select data and set value to boolean based on timestamp column in MySQL
- Select minimum row value from a column with corresponding duplicate column values in MySQL
- How to revert rows to default column value in MySQL?
- Select from another column if selected value is '0' in MySQL?
- How to remove rows containing missing value based on a particular column in an R data frame?

Advertisements