Select all duplicate rows based on one or two columns?

user3286692

I Have a table named contacts with fields

+-----+------------+-----------+
| id  | first_name | last_name |
+-----+------------+-----------+

I want to display all duplicates based on first_name and (/ or) last_name, e.g:

+----+------------+-----------+
| id | first_name | last_name |
+----+------------+-----------+
|  1 | mukta      | chourishi |
|  2 | mukta      | chourishi |
|  3 | mukta      | john      |
|  4 | carl       | thomas    |
+----+------------+-----------+

If searched on just first_name it should return:

+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
+----+

But if searched on both first_name and last_name should return:

+----+
| id |
+----+
|  1 |
|  2 |
+----+
Grijesh Chauhan

One way to achieve your result is using nested query and having clause: In inner query select those having count more then one, and in outer query select id:

Check following example for single column selection criteria:

Create table:

CREATE TABLE `person` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `first` varchar(120) NOT NULL,
    `last` varchar(120) NOT NULL
);

Insert tuple:

INSERT INTO `person` ( `first`, `last`) VALUES
("mukta", "chourishi"),
("mukta", "chourishi"),
("mukta", "john"),
("carl", "thomas" );

The result you need:

mysql> SELECT  `id` 
    -> FROM `person` 
    -> WHERE `first`=(SELECT `first` FROM `person` HAVING COUNT(`first`) > 1);
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
+----+
3 rows in set (0.00 sec)

[ANSWER]

But as if you selection criteria is on the basis of more than one columns then you can make use of JOIN.

To explain it I am writing a selection query that creates an intermediate table that will be use in JOIN as second operand table.

Query is select all fist name and column those duplicates with some of other rows:
For example select rows in which first and last name repeats

mysql> SELECT `first`, `last`,  count(*)  as rows 
    -> FROM `person` 
    -> GROUP BY `first`, `last` 
    -> HAVING count(rows) > 1;
+-------+-----------+------+
| first | last      | rows |
+-------+-----------+------+
| mukta | chourishi |    2 |
+-------+-----------+------+
1 row in set (0.00 sec)

So you have only one pair of first and last names those repeats (or is duplicates with some other rows).

Now, question is: how to select id of this row? Use Join! as follows:

mysql> SELECT  p1.`id`
    -> FROM `person` as p1
    -> INNER JOIN (
    ->     SELECT `first`, `last`,  count(*)  as rows
    ->     FROM `person` 
    ->     GROUP BY `first`, `last` 
    ->     HAVING count(rows) > 1) as p
    -> WHERE p.`first` = p1.`first` and p.`last` = p1.`last`;  
+----+
| id |
+----+
|  1 |
|  2 |
+----+
2 rows in set (0.06 sec)

you can select on the basis of as many columns as you wants e.g. single column if you want using join then remove last name.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to select duplicate rows and group by two columns

From Dev

mysql select rows based on two columns

From Dev

How to Select rows based on two columns

From Dev

SQL two rows into one based on three columns

From Dev

How to select rows from MySQL based on max value of a one column and grouping two other columns?

From Dev

R - find and list duplicate rows based on two columns

From Dev

excel: check for duplicate rows based on 3 columns and keep one row

From Dev

Removing all rows of a duplicate based on value of multiple columns

From Dev

Query to delete all duplicate rows but one where no columns is/are unique

From Dev

SQL to select all rows with duplicate values in one column

From Dev

SQL: SELECT value for all rows based on a value in one of the rows and a condition

From Dev

TSQL Duplicate rows based on columns

From Dev

SQL: Duplicate rows based on columns

From Dev

Mysql need to select range of rows based on two columns values

From Dev

Select pandas frame rows based on two columns' values

From Dev

How to select rows based on join on two columns of same table

From Dev

MySQL select rows based on a result between two date columns

From Dev

How to select rows based on join on two columns of same table

From Dev

Select ALL Columns | Rows of a data.table based on condition

From Dev

pandas: How do I select rows based on the sum of all columns?

From Dev

Excel Pivot Table select rows based on column, with all columns displayed

From Dev

On duplicate key based on two columns

From Dev

On duplicate key based on two columns

From Dev

Select records where all rows have same value in two columns

From Dev

Select all rows where two columns contain a combination of values

From Dev

Select all rows but one

From Dev

Select max of one column based on two other columns

From Dev

Removing DUPLICATE rows in hive based on columns

From Dev

Pandas- Removing duplicate rows based on the columns

Related Related

  1. 1

    How to select duplicate rows and group by two columns

  2. 2

    mysql select rows based on two columns

  3. 3

    How to Select rows based on two columns

  4. 4

    SQL two rows into one based on three columns

  5. 5

    How to select rows from MySQL based on max value of a one column and grouping two other columns?

  6. 6

    R - find and list duplicate rows based on two columns

  7. 7

    excel: check for duplicate rows based on 3 columns and keep one row

  8. 8

    Removing all rows of a duplicate based on value of multiple columns

  9. 9

    Query to delete all duplicate rows but one where no columns is/are unique

  10. 10

    SQL to select all rows with duplicate values in one column

  11. 11

    SQL: SELECT value for all rows based on a value in one of the rows and a condition

  12. 12

    TSQL Duplicate rows based on columns

  13. 13

    SQL: Duplicate rows based on columns

  14. 14

    Mysql need to select range of rows based on two columns values

  15. 15

    Select pandas frame rows based on two columns' values

  16. 16

    How to select rows based on join on two columns of same table

  17. 17

    MySQL select rows based on a result between two date columns

  18. 18

    How to select rows based on join on two columns of same table

  19. 19

    Select ALL Columns | Rows of a data.table based on condition

  20. 20

    pandas: How do I select rows based on the sum of all columns?

  21. 21

    Excel Pivot Table select rows based on column, with all columns displayed

  22. 22

    On duplicate key based on two columns

  23. 23

    On duplicate key based on two columns

  24. 24

    Select records where all rows have same value in two columns

  25. 25

    Select all rows where two columns contain a combination of values

  26. 26

    Select all rows but one

  27. 27

    Select max of one column based on two other columns

  28. 28

    Removing DUPLICATE rows in hive based on columns

  29. 29

    Pandas- Removing duplicate rows based on the columns

HotTag

Archive