简体   繁体   English

如何在MySQL数据库中查找与其他行没有正确关联的行

[英]How to find rows in a MySQL database that don't correlate correctly with other rows

I have a table in my MySQL database that, in part, looks like this: 我的MySQL数据库中有一个表,部分看起来像这样:

+----+-------+-------+
| id | owner | onwed |
+----+-------+-------+
|  1 |   A   |   B   |
|  2 |   B   |   A   |
|  3 |   C   |   D   |
|  4 |   D   |   C   |
|  5 |   E   |   X   |
+----+-------+-------+

They important feature here is that all the entries are paired, so that each "owner" is also "owned" by the record they correlate to. 它们的重要特征是所有条目都是成对的,因此每个“所有者”也由它们关联的记录“拥有”。 A "owns" B, and also B "owns" A. D "owns" C, and also C "owns" D. A“拥有” B,并且B“拥有”A。D“拥有” C,也C“拥有”D。

However, in row 5,we have a problem. 但是,在第5行中,我们有一个问题。 E "owns" X, but there is no entry for X "owning" E. E“拥有” X,但是没有X“拥有” E的条目。

I need to be able to go through this table, which has thousands of records, and find all instances like row 5, where we have an orphaned record. 我需要能够浏览具有数千条记录的该表,并找到第5行之类的所有实例,在这些表中,我们有一个孤立的记录。 Where there is no corralating with the "owner" and "owned" fields having a matched opposite. 在没有关联的情况下,“所有者”和“拥有”字段具有相反的匹配关系。 Also, there is no assurance that paired rows will follow each other as they do in my example. 同样,不能保证成对的行会像在我的示例中那样紧随其后。

This problem goes way beyond my MySQL abilities. 这个问题已经超出了我的MySQL能力的方式 I know how to do a search when I know what the value I'm looking for is. 当我知道所寻找的价值是什么时,我便知道如何进行搜索。 But I don't know how to go through each row one by one, take values out, and then use those values to do another search. 但是我不知道如何一一遍遍每一行,取出值,然后使用这些值进行另一次搜索。 I hope someone can help me out and I apologize that I'm so clueless on this matter that I don't have any code to suggest. 我希望有人能帮助我,对不起,我对此事一无所知,所以我没有任何代码可言。

I'm not so worried about efficiency, in that this is a check I would only run every now and again when there is reason to suspect a problem. 我并不担心效率,因为这是一项检查,只有在有理由怀疑有问题时,我才会不时地进行检查。 Also, if it helps, I manage my MySQL database from a PHP script, so if there is PHP code that can be leveraged to make the task more manageable, that could also be utilized. 另外,如果有帮助,我可以通过PHP脚本管理MySQL数据库,因此,如果可以利用PHP代码使任务更易于管理,那么也可以使用该代码。

What you need is a join . 您需要的是join If you join the table on itself with the owned on owner. 如果您将表本身与拥有的所有者一起加入。

SELECT T1.Id, T1.Owner, T1.Owned, T2.Id, T2.Owner, T2.Owned 
FROM tablename T1 
LEFT JOIN tablename T2
ON T1.Owned = T2.Owner
WHERE T2.Owned != T1.Owner
OR T2.Id IS NULL

Try to run the query without the WHERE clause to see what this join does. 尝试运行不带WHERE子句的查询,以查看此联接的作用。 You get both owners and owned on a single row and then you can compare if they match. 您既拥有所有者又拥有同一行,然后可以比较它们是否匹配。

You could try a query similar to (not sure about the backquotes of MySQL) 您可以尝试类似的查询(不确定MySQL的反引号)

SELECT * FROM `table` 
  WHERE (`owner`,`owned`) 
  NOT IN (SELECT `owned`,`owner` FROM `table`);

There are probably cleaner solutions without selfjoins 可能有没有自连接的更干净的解决方案

There are better ways, but this will work: 有更好的方法,但这将起作用:

SELECT
    *
FROM
    table
WHERE
    id NOT IN (
        SELECT
            id
        FROM
            table s1
        WHERE
            s1.owner = (
                    SELECT
                        onwed
                    FROM
                        table s2
                    WHERE
                        s2.onwed = s1.owner
            )
    );

Its easy: 这很容易:

select t1.id from tableName as t1 left join tableName as t2 
    on t1.owner = t2.owned and t1.owned = t2.owner 
    where t2.id is null
select 
    * 
from 
    mytable 

where id not in (
    select
        m1.id
    from
        mytable m1,
        mytable m2

    where 
        m1.owner=m2.owned    
)   

You can try this- demo below : 您可以尝试以下演示:

http://sqlfiddle.com/#!2/0db47/7 http://sqlfiddle.com/#!2/0db47/7

SELECT  a.id, a.owner, a.onwed
FROM    TableName a
        LEFT JOIN TableName b
          ON  (a.Owner = b.Owner AND a.Onwed = b.OnWed) OR
              (a.Owner = b.OnWed AND a.Onwed = b.Owner)
GROUP   BY  a.id, a.owner, a.onwed
HAVING  COUNT(*) = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM