我正在编写一个函数来删除与某个查询匹配的所有行。
如果我选择匹配的行ID,然后分两步删除带有id的行,那么它就可以了。因此,我知道我对查找要删除的行的查询是正确的:
sql = """SELECT MSN.id
FROM schedule_missingschedulenames as MSN,
names_parsedname as PN,
names_simplifiednamemapping as SNM,
names_canonicalname as CN
WHERE MSN.parsed_name_id = PN.id
AND PN.simplified_name = SNM.simplified_name
AND SNM.canonical_name_id = CN.id
AND CN.category_id = %s""" % cat.id
ids = [row.id for row in MissingScheduleNames.objects.raw(sql)]
MissingScheduleNames.objects.filter(id__in=ids).delete()
但是,如果我尝试在一个查询中执行DELETE,它就不起作用:
sql = """DELETE FROM schedule_missingschedulenames WHERE id in (SELECT MSN.id
FROM schedule_missingschedulenames as MSN,
names_parsedname as PN,
names_simplifiednamemapping as SNM,
names_canonicalname as CN
WHERE MSN.parsed_name_id = PN.id
AND PN.simplified_name = SNM.simplified_name
AND SNM.canonical_name_id = CN.id
AND CN.category_id = %s)""" % cat.id
MissingScheduleNames.objects.raw(sql)
第二个查询根本不会删除任何内容。
DELETE查询在哪里出错了?