gpt4 book ai didi

php - Mysql Select查找重复项

转载 作者:可可西里 更新时间:2023-11-01 07:47:53 25 4
gpt4 key购买 nike

有人可以帮我写一个 sql select 来执行任务吗?所以问题是我们有一张表并且有一些重复项,所以我需要找到 name、street 和 house 相同的地方并以某种方式对它们进行分组。

我差不多this情况,但不同之处在于我想将它们分组以查找重复的内容。

提前致谢。

最佳答案

假设您有一个 id 字段,它将与 GROUP_CONCAT() 分组每个重复行的函数:

SELECT    t1.name, t1.street, t1.house, GROUP_CONCAT(DISTINCT t1.id) dupes
FROM your_table t1
JOIN your_table t2 ON (t2.name = t1.name AND
t2.street = t1.street AND
t2.house = t1.house)
GROUP BY t1.name, t1.street, t1.house
HAVING COUNT(*) > 1;

测试用例:

CREATE TABLE your_table (
id int,
name varchar(10),
street varchar(10),
house varchar(10)
);

INSERT INTO your_table VALUES (1, 'a', 'b', 'c');
INSERT INTO your_table VALUES (2, 'a', '1', 'c');
INSERT INTO your_table VALUES (3, 'a', '2', '3');
INSERT INTO your_table VALUES (4, 'a', 'b', 'c');
INSERT INTO your_table VALUES (5, 'a', 'b', 'c');
INSERT INTO your_table VALUES (6, 'c', 'd', 'e');
INSERT INTO your_table VALUES (7, 'c', 'd', 'e');

结果:

+------+--------+-------+-------+
| name | street | house | dupes |
+------+--------+-------+-------+
| a | b | c | 1,5,4 |
| c | d | e | 6,7 |
+------+--------+-------+-------+
2 rows in set (0.03 sec)

关于php - Mysql Select查找重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3501265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com