डुप्लिकेट का चयन करने के लिए, आप सबक्वेरी का उपयोग कर सकते हैं। आइए पहले एक टेबल बनाएं -
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(100) -> ); Query OK, 0 rows affected (0.87 sec)
इंसर्ट कमांड का उपयोग करके टेबल में कुछ रिकॉर्ड डालें -
mysql> insert into DemoTable(Name) values('John'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(Name) values('Chris'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(Name) values('John'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Name) values('David'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Name) values('Bob'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Name) values('Chris'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(Name) values('Mike'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Name) values('Robert'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(Name) values('Mike'); Query OK, 1 row affected (0.12 sec)
चयन कथन का उपयोग करके तालिका से सभी रिकॉर्ड प्रदर्शित करें -
mysql> select *from DemoTable;
आउटपुट
+----+--------+ | Id | Name | +----+--------+ | 1 | John | | 2 | Chris | | 3 | John | | 4 | David | | 5 | Bob | | 6 | Chris | | 7 | Mike | | 8 | Robert | | 9 | Mike | +----+--------+ 9 rows in set (0.00 sec)
सभी डुप्लीकेट चुनने के लिए क्वेरी निम्नलिखित है -
mysql> select *from DemoTable -> where Name in (select Name from DemoTable group by Name having count(*) > 1);
आउटपुट
+----+-------+ | Id | Name | +----+-------+ | 1 | John | | 2 | Chris | | 3 | John | | 6 | Chris | | 7 | Mike | | 9 | Mike | +----+-------+ 6 rows in set (0.09 sec)