php—在表中查找重复记录及其相关的其他重复项

qojgxg4l  于 2021-06-20  发布在  Mysql
关注(0)|答案(5)|浏览(259)

我试图在我的表中找到重复的记录,我有代码来显示记录,但我需要修改它,以便它也显示另一个重复的记录。我的示例表如下:
表格图像
如您所见,表(t\u sen)包含重复记录,我使用以下代码来显示重复记录:

$sql = "SELECT ID, PARA_NUMBER, TEXT FROM t_sen GROUP BY TEXT having count(*) >= 2";

    $results = mysqli_query($conn, $sql) or die(mysqli_error());

    while($row = mysqli_fetch_assoc($results)){

        foreach($row as $column => $value){
            print "$column: $value <br>";
        }

        print "<br>";
    }

我得到的结果是:

ID  |   PARA_NUMBER  |  TEXT
----------------------------
3   |   1            |  is
7   |   2            |  live

但是,我想要的输出是:

ID  |   PARA_NUMBER  |  TEXT
----------------------------
3   |   1            |  is
4   |   1            |  is

7   |   2            |  live
8   |   2            |  live

我想让它显示另一个重复的列也在定义的para\ u编号内。因此,一个(编造的?)查询示例如下:

SELECT ID, PARA_NUMBER, TEXT FROM t_sen (WITHIN PARA_NUMBER = 1) having count(*) >= 2

所以输出应该是:

ID  |   PARA_NUMBER  |  TEXT
----------------------------
3   |   1            |  is
4   |   1            |  is

但是如果我不必对每个参数都重复查询,那就更好了,因为有许多参数,而且只有一个查询可以显示所有重复的记录。
希望我清楚。任何帮助都将不胜感激。

zaq34kh6

zaq34kh61#

左连接

SELECT
    t1.*
FROM
    t_sen AS t1
LEFT JOIN 
    t_sen AS t2 ON t1.ID != t2.ID AND t1.PARA_NUMBER = t2.PARA_NUMBER 
WHERE
    t2.ID IS NOT NULL

小提琴 t1.ID != t2.ID 排除两个表中匹配的同一行(以及连接的两侧) t1.PARA_NUMBER = t2.PARA_NUMBER 在重复的数字上加入故事 WHERE t2.ID IS NOT NULL 排除不重复的记录。
你也可以用 TEXT 对于一个列来说,这是一个非常糟糕的名字,通过将它添加到连接的on部分,addition或inspect of PARA_NUMBER 如你所需。
只是为了子孙后代

CREATE TABLE t_sen (
    ID INT,
    PARA_NUMBER  INT,
    `TEXT` VARCHAR(20)
);

# non duplate

INSERT INTO t_sen  (ID, PARA_NUMBER, `TEXT`)VALUES(1,10,'the');

# duplicates

INSERT INTO t_sen  (ID, PARA_NUMBER, `TEXT`)VALUES(3,1,'is');
INSERT INTO t_sen  (ID, PARA_NUMBER, `TEXT`)VALUES(4,1,'is');
INSERT INTO t_sen  (ID, PARA_NUMBER, `TEXT`)VALUES(7,2,'live');
INSERT INTO t_sen  (ID, PARA_NUMBER, `TEXT`)VALUES(8,2,'live');
ifsvaxew

ifsvaxew2#

下面的查询应该打印所有在段落号和文本字段上有重复的记录-
选择id,段落编号,文本来自t\u sen,其中(段落编号,文本)位于(选择段落编号,文本来自t\u sen group by段落编号,文本具有count(*)>=2)

sr4lhrrt

sr4lhrrt3#

此查询将执行您想要的操作。它使用子查询来查找 TEXT 表中有重复项的,并将其与原始表联接,以便可以显示每个有重复项的条目 TEXT 价值观。

SELECT ID, PARA_NUMBER, t1.TEXT FROM
t_sen t1
JOIN (SELECT TEXT
      FROM t_sen
      GROUP BY TEXT
      HAVING COUNT(*) >= 2) t2
ON t1.TEXT = t2.TEXT

输出:

ID  PARA_NUMBER     TEXT
3   1               is
4   1               is
7   2               live
8   2               live

sqlfiddle演示
如果你只想把复制品弄进来 PARA_NUMBER 1,例如,只需添加 WHERE 从头到尾:

SELECT ID, PARA_NUMBER, t1.TEXT FROM
t_sen t1
JOIN (SELECT TEXT
      FROM t_sen
      GROUP BY TEXT
      HAVING COUNT(*) >= 2) t2
ON t1.TEXT = t2.TEXT
WHERE PARA_NUMBER = 1

输出:

ID  PARA_NUMBER     TEXT
3   1               is
4   1               is

编辑
基于ops要求能够编辑phpmyadmin中的行,使用 JOIN 解决不了问题。相反,需要这样的查询:

SELECT ID, PARA_NUMBER, t1.TEXT FROM
t_sen t1
WHERE EXISTS (SELECT *
              FROM t_sen t2
              WHERE t2.PARA_NUMBER = t1.PARA_NUMBER AND
                    t2.TEXT = t1.TEXT AND
                    t2.ID != t1.ID)

输出:

ID  PARA_NUMBER     TEXT
3   1               is
4   1               is
7   2               live
8   2               live
2exbekwf

2exbekwf4#

我总是用分组来寻找重复的。我的建议是:

SELECT GROUP_CONCAT(CONCAT_WS('-', ID, PARA_NUMBER, TEXT)) AS data FROM t_sen GROUP BY PARA_NUMBER HAVING COUNT(*) > 2

这将为您提供以下格式的结果:

| data          |
| 3-1-is,4-1-is |
7kqas0il

7kqas0il5#

SELECT t.ID, t.PARA_NUMBER, t.TEXT FROM
(SELECT TEXT FROM t_sen GROUP BY TEXT having count(*) >= 2) as duplicateValue
LEFT JOIN t_sen as t on duplicateValue.TEXT = t.TEXT
WHERE 1;

相关问题