[英]Many-to-many join table, bi-directional relationships and data integrity
I have an old legacy table like this: 我有一个像这样的旧遗留表:
=====================
| product1 | product2 |
=====================
| 1 | 2 |
| 2 | 1 |
| 3 | 4 |
| 4 | 5 |
| 5 | 3 |
=====================
It's a poorly designed table, I know, but it has to stay. 我知道这是一张设计不良的桌子,但必须保留。 The purpose of the table is to store relationships between products.
该表的目的是存储产品之间的关系。 The rule is that each product should have a reference to each similar product and vice-versa.
规则是每个产品都应引用每个相似产品,反之亦然。 Assuming that the example data is the full dataset, the first two rows are correct - 1 has a relationship with 2 and 2 has a relationship with 1. However, the last three rows are a mess.
假设示例数据是完整数据集,则前两行是正确的-1与2有关系,而2与1有关系。但是,最后三行是一团糟。 The correct way of representing those products' relationships would be:
表示这些产品的关系的正确方法是:
=====================
| product1 | product2 |
=====================
| 3 | 4 |
| 3 | 5 |
| 4 | 3 |
| 4 | 5 |
| 5 | 3 |
| 5 | 4 |
=====================
What I need is some kind of algorithm (or tool) so that I can identify and fix the incorrect data. 我需要某种算法(或工具),以便我可以识别和修复错误的数据。
That's a relatively straightforward SQL query. 那是一个相对简单的SQL查询。
SELECT product1,product2 ... WHERE its mirror row does not exist. SELECT product1,product2 ...在其镜像行不存在的地方。
and that latter clause is like 那后一条就像
NOT EXISTS ( SELECT * FROM tbl AS MIRROR WHERE MIRROR.product1 = outertbl.product2 AND MIRROR.product2 = outertbl.product1 ) 不存在(从* tbl中选择*作为镜像的镜像,其中MIRROR.product1 = externaltbl.product2和MIRROR.product2 = externaltbl.product1)
If MySQL supports EXCEPT, that's another way to write the query. 如果MySQL支持EXCEPT,则这是编写查询的另一种方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.