Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 避免/删除表中的重复行_Mysql_Sql - Fatal编程技术网

Mysql 避免/删除表中的重复行

Mysql 避免/删除表中的重复行,mysql,sql,Mysql,Sql,我有一张友谊桌 CREATE TABLE IF NOT EXISTS `friendList` ( `id` int(10) NOT NULL, `id_friend` int(10) NOT NULL, KEY `id` (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; 我有一些条目,如: 条目A:1-2 条目B:2-1 条目C:1-2 条目A和B显示用户1是用户2的朋友。但是条目C是无用的,所以我需要删除它 有没有办法删除所有重复的行

我有一张友谊桌

CREATE TABLE IF NOT EXISTS `friendList` (
  `id` int(10) NOT NULL,
  `id_friend` int(10) NOT NULL,
  KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
我有一些条目,如:

条目A:1-2

条目B:2-1

条目C:1-2

条目A和B显示用户1是用户2的朋友。但是条目C是无用的,所以我需要删除它

有没有办法删除所有重复的行或应用特定的约束来避免这种情况


谢谢

您可以为这两列添加唯一索引以强制唯一性:

alter table friendList add unique index(id, id_friend);

什么是您的表结构>参考此链接问题是id键不是唯一的,也不是唯一的。我正在寻找另一种解决方案,而不是添加唯一的主键,但这可能是不可能的。谢谢你的回答。嗨,我也希望条目3-1或1-3是可能的。我只是不希望两行有相同的x-y对x=give int,y=give int。这没关系,因为3-1和1-3不一样。是的,它起作用了,我想我只是不太明白什么是唯一索引。谢谢: