我有一个数据库表bad_reviews
和一个对应的Django模型BadReviews
。我想删除基于字段client_id
,survey_id
,text
,rating
,privacy_agreement
的重复记录。我提出了这个查询:
SELECT br.*
FROM bad_reviews br
JOIN (
SELECT client_id, survey_id, text, rating, privacy_agreement, COUNT(*)
FROM bad_reviews
GROUP BY client_id, survey_id, text, rating, privacy_agreement
HAVING count(*) > 1
) dupes
ON br.client_id = dupes.client_id
AND br.survey_id = dupes.survey_id
AND br.text = dupes.text
AND br.rating = dupes.rating
AND br.privacy_agreement = dupes.privacy_agreement
ORDER BY br.client_id, br.survey_id, br.text, br.rating, br.privacy_agreement, br.id
如何使用Django ORM重写它?
1条答案
按热度按时间jvlzgdj91#
我希望这能奏效。