在Mysql中找到同一行内多列的重复值

时间:2017-12-01 08:34:31

标签: php mysql sql

请帮助在MySQL中找到同一行多列中的重复条目:

table description

3 个答案:

答案 0 :(得分:0)

如果要查找列中包含重复项的记录,可以使用此查询:

SELECT T1.* FROM tbl T1
JOIN  
    (SELECT id
    FROM (
        SELECT id, sample1 AS n from tbl
        UNION ALL
        SELECT id, sample2 AS n from tbl
        UNION ALL
        SELECT id, sample3 AS n from tbl
    ) AS X
    GROUP BY id, n
    HAVING COUNT(*) > 1
) T2
    ON T1.id = T2.id;

您还可以对其进行测试Here

答案 1 :(得分:-1)

我不确定MySql

但是在下面的PHP示例中将使用。

示例

$query = "select * from table_name";
$result = mysqli_query($query);
while ($row = mysqli_fetch_array($result)) {
    if ($row['sample1'] == $row['sample2']) {
        // This row duplicate
    }
    else {
        // This row not duplicate
    }
}

答案 2 :(得分:-2)

SELECT Sample1, COUNT(*) C FROM tablename GROUP BY Sample1 HAVING C > 1;

Finding duplicate values in MySQL

MySQL select records for duplicates using multiple columns