从主表中删除重复项,还要从关系表中删除重复项?

时间:2018-12-16 04:06:57

标签: mysql sql

我有两个表questionschoiceschoicesquestions有一个名为question_id的列。此外,它在问题表上也有外部约束,而在删除时则有级联。

但是我设法在数据库中获得重复项。删除起来相当简单,但是我还必须检查choices表。然后检查看看它们是否也是重复的...

questions表中,列descriptionimage中的匹配内容将重复,而在表choices中,该列的choice列问题,是否有意义?

因此,我认为它实际上可以归结为choices表。 (对不起,我真的不明白我是怎么开始遇到这个问题的。)

因为questions“说明”列可以相同。但是与图像有关,答案取决于图像显示的内容。有时有时会出现带有描述的重复图像,对所有图像进行图像显示,但是选择的顺序不同。

很难解释。但是有趣的字段是descriptionsourceimage。和choice表中的choices,但它们可以采用不同的顺序。

我已经用我认为相关的数据完成了SQLfiddle

CREATE TABLE IF NOT EXISTS `questions` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `order` int(11) NOT NULL DEFAULT 1,
  `description` text DEFAULT NULL,
  `source` text DEFAULT NULL,
  `image` text DEFAULT NULL,
  PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `choices` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `question_id` int(10) unsigned NOT NULL,
  `choice` text DEFAULT NULL,
  `correct` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `choices_question_id_foreign` (`question_id`),
  CONSTRAINT `choices_question_id_foreign` FOREIGN KEY (`question_id`) REFERENCES `questions` (`id`) ON DELETE CASCADE
) DEFAULT CHARSET=utf8;

INSERT INTO `questions` (`order`, `description`, `source`, `image`) VALUES
  ('1', 'Is the earth is flat?', null, 'eerrff.png'), #ID 1 (NOT DUPLICATE, NO SOURCE?)
  ('1', 'Is the earth is flat?', 'In a very special community, it is', 'eerrff.png'), #ID 2 (DUPLICATES)
  ('1', 'Is the earth is flat?', 'In a very special community, it is', 'eerrff.png'), #ID 3 (DUPLICATES)
  ('1', 'Is the earth is flat?', 'In a very special community, it is', 'eerrff.png'), #ID 4 (DUPLICATES)
  ('1', 'Is the earth is flat?', 'In a very special community, it is', 'eerrff.png'), #ID 5 (DUPLICATES)
  ('1', 'Is the earth is flat?', 'In a very special community, it is', 'eerrff.png'), #ID 6 (DUPLICATES)
  ('1', 'Is the earth is flat?', 'In a very special community, it is', 'eerrff.png'), #ID 7 (DUPLICATES)
  ('1', 'Is the earth is flat?', 'In a very special community, it is', 'eerrff.png'), #ID 8 (DUPLICATES)
  ('1', 'Is the earth is flat?', 'In a very special community, it is', 'eerrff.png'), #ID 9 (DUPLICATES)
  ('1', 'Look at the image, what is correct?', 'source', 'oopp.png'), #ID 10 (DUPLICATE WITH #12)
  ('1', 'Look at the image, what is correct?', 'source', 'qqww.png'), #ID 11 (NOT DUPLICATE, LOOK CHOICES)
  ('1', 'Look at the image, what is correct?', 'source', 'oopp.png'), #ID 12 (DUPLICATE WITH #10)
  ('1', 'Look at the image, what is correct?', 'source', 'oopp.png'); #ID 13 (NOT DUPLICATE, LOOK CHOICES)

INSERT INTO `choices` (`question_id`, `choice`, `correct`) VALUES
  ('1', 'Yes', 1),
  ('1', 'No', 0),
  ('2', 'Yes', 1),
  ('2', 'No', 0),
  ('3', 'No', 0),
  ('3', 'Yes', 1),
  ('4', 'Yes', 1),
  ('4', 'No', 0),
  ('5', 'No', 0),
  ('5', 'Yes', 1),
  ('6', 'Yes', 1),
  ('6', 'No', 0),
  ('7', 'Yes', 1),
  ('7', 'No', 0),
  ('8', 'No', 0),
  ('8', 'Yes', 1),
  ('9', 'Yes', 1),
  ('9', 'No', 0),
  ('10', 'First is corrct', 1),
  ('10', 'second', 0),
  ('10', 'third', 0),
  ('10', 'fourth', 0),
  ('11', 'b', 0),
  ('11', 'c', 0),
  ('11', 'Number A', 1),
  ('11', 'd', 0),
  ('12', 'third', 0),
  ('12', 'second', 0),
  ('12', 'First is corrct', 1),
  ('12', 'fourth', 0),
  ('13', 'That guy on the left is doin it wrong', 0),
  ('13', 'That guy on the right is doin it wrong', 0),
  ('13', 'That guy in yellow is doin in right', 1),
  ('13', 'That guy green is doin it right', 0);

预期结果:根据questions产生的不同行,从choices表中删除重复项。

1 个答案:

答案 0 :(得分:0)

下面将为您提供所有具有相同选择的不同问题的question_id的列表:

select distinct c1.* from choices c1 join choices c2 
on c1.question_id != c2.question_id 
and c1.choice = c2.choice 
order by choice