mysql - 数据库多对多关系完整性更新
问题描述
数据库有order
和customer
表,以及order_customer
多对多表。为简单起见,假设order
hasid
和customer_id
(后一个字段对于多对多关系不需要,但由于历史原因保留)字段和hasorder_customer
和id
字段。此外,所有表都有字段(是的,它是 laravel)——如果该行没有被删除,则为 NULL。
我做了一些完整性检查,以了解是否需要旧字段。 order_id
customer_id
deleted_at
customer_id
select `order`.`id`, `order`.`customer_id`
from `order`
where
`order`.customer_id not in (
select customer_id from order_customer
where `order_customer`.deleted_at is null
#and `order_customer`.order_id = `order`.`order_id`
)
and
`order`.deleted_at is null;
返回 25 行。所以我只是order.customer_id
在帐户中添加了该字段。
但现在我被要求修复order
和order_customer
表中的所有完整性问题。所以我做了一个简单的程序python
来检查所有 3 种情况:
order.custmer_id
不存在于select customer_id from order_customer where order_id = order.id
order_customer.order_id
不存在于select id from order
order.id
不存在于select order_id from order_customer
import mysql.connector
config = {
'user': 'root',
'password': '***',
'host': '127.0.0.1',
'database': '***',
'raise_on_warnings': True
}
cnx = mysql.connector.connect(**config)
cursor = cnx.cursor()
query = ("select `order`.`id`, `order`.`customer_id` "
"from `order` "
"where `order`.deleted_at is null"
)
cursor.execute(query)
orders=list(cursor)
query = ("select `order_customer`.`order_id`,`order_customer`.`customer_id` "
"from `order_customer` "
"where `order_customer`.deleted_at is null"
)
cursor.execute(query)
m2m=list(cursor)
cursor.close()
cnx.close()
print len(set(c for i,c in orders)-set(c for o,c in m2m))
25
>>> from collections import defaultdict
>>> d=defaultdict(set)
>>> for o,c in m2m:
d[o].add(c)
>>> len(d)
40983
>>> len(set(i for i,c in orders if i in d and c not in d[i]))
37
>>> s=set(i for i,c in orders);x=[(o,c) for o,c in m2m if o not in s]
>>> len(x)
1377
>>> x[:10]
[(15968, 2), (98, 3), (109, 3), (115, 3), (1199, 3), (1200, 3), (3626, 3), (3843, 3), (4303, 3), (4681, 3)]
>>> max(o for o,c in x)
37653
>>> len(m2m)
42122
>>> len(orders)
39628
但我需要这些东西SQL
。
主要部分-->
select `order`.id,`order`.customer_id,
(select
`order_customer`.customer_id
from `order_customer` where
`order_customer`.order_id = `order`.id) as customer_ids
from `order`
where
customer_id not in customer_ids;
根本不起作用,但我需要类似的东西来包含在insert
/update
查询中。谢谢。
解决方案
推荐阅读
- python-3.x - 每次迭代除以2时如何打印数字的余数(userInput)
- python - 为什么使用 Numba 时这段代码不更快?
- flutter-web - 是否可以将 OneSignal 与 Flutter-Web 一起使用?
- entity-framework - 如何将多个查询应用于实体框架中的所有数据库集?
- html - 我有两个在 HTML 和 CSS 中相同的 CodePens,但它们在浏览器窗口中的输出不同
- asp.net - Asp.Net Core 5.0:如何删除与 Azure 数据库 SQL 的连接?
- angular - ng-alain 使用 SFUISchema 呈现密码字段
- c# - 我想将 messageBoxIcons 添加到图片框图像上
- reactjs - Core Ui CIcon 箭头未切换
- c# - 使用正则表达式识别包含单个句点的字符串