我有一个名为与字段联系的表
+-----+------------+-----------+
| id | first_name | last_name |
+-----+------------+-----------+
我想基于first_name和(/或)last_name显示所有重复项,例如:
+----+------------+-----------+
| id | first_name | last_name |
+----+------------+-----------+
| 1 | mukta | chourishi |
| 2 | mukta | chourishi |
| 3 | mukta | john |
| 4 | carl | thomas |
+----+------------+-----------+
如果仅在first_name上搜索它应该返回:
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
+----+
但是如果在first_name和last_name上搜索都应该返回:
+----+
| id |
+----+
| 1 |
| 2 |
+----+
解决方法:
实现结果的一种方法是使用嵌套查询和having子句:在内部查询中选择那些计数多于一的那些,并在外部查询中选择id:
检查以下示例的单列选择标准:
创建表格:
CREATE TABLE `person` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`first` varchar(120) NOT NULL,
`last` varchar(120) NOT NULL
);
插入元组:
INSERT INTO `person` ( `first`, `last`) VALUES
("mukta", "chourishi"),
("mukta", "chourishi"),
("mukta", "john"),
("carl", "thomas" );
您需要的结果:
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)
[回答]
但好像您的选择标准是基于多个列,那么您可以使用JOIN.
为了解释它,我正在编写一个选择查询,该查询创建一个将在JOIN中用作第二个操作数表的中间表.
查询是选择所有拳头名称和列重复与其他一些行:
例如,选择重复名字和姓氏的行
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)
因此,您只有一对名字和姓氏重复(或与其他一些行重复).
现在,问题是:如何选择此行的ID?使用加入!如下:
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)
您可以根据需要选择任意数量的列,例如单列如果要使用连接,则删除姓氏.