MySQL:限制列中的n个重复项

时间:2017-05-30 12:56:26

标签: mysql duplicates

以下几个问题:

  1. 有没有办法限制特定列中的重复数量?是否有可用的列属性或设置?

  2. 在导入数据后运行时是否存在任何SQL查询,请通过保留该列的前n个重复项来删除重复项。

  3. 感谢您对此的建议和指导。

1 个答案:

答案 0 :(得分:1)

<强> 1

也许这不是最好的解决方案,但是像一个选项,你可以使用触发器,如下所示:

create table test (msg varchar(25));

delimiter |
create trigger test_duplicates before insert on test
for each row
begin
  if (select count(*) from test where msg = new.msg) >= 3 then
    signal sqlstate '45000' set message_text = 'You cannot insert 4th duplicate.';
  end if;
end;
|
delimiter ;

它会像这样工作:

mysql> insert into test values ('this is test');
Query OK, 1 row affected (0.03 sec)

mysql> insert into test values ('this is test');
Query OK, 1 row affected (0.03 sec)

mysql> insert into test values ('this is test');
Query OK, 1 row affected (0.02 sec)

mysql> insert into test values ('this is test');
ERROR 1644 (45000): You cannot insert 4th duplicate.
mysql> select * from test;
+--------------+
| msg          |
+--------------+
| this is test |
| this is test |
| this is test |
+--------------+
3 rows in set (0.00 sec)

<强> 2

使用目的导入数据 - 您可以在导入开始时禁用触发器,然后导入所有重复项,然后删除重复项,然后启用触发器,如下所示:

select * from test;
+----------------+
| msg            |
+----------------+
| this is test   |
| this is test   |
| this is test   |
| this is test   |
| this is test 2 |
| this is test 2 |
| this is test 2 |
| this is test 2 |
| this is test 3 |
| this is test 3 |
| this is test 3 |
| this is test 4 |
+----------------+

将重复项放在临时表中:

create temporary table tmp like test;
insert into tmp select msg from test group by msg having count(msg) > 3;
select * from tmp;
+----------------+
| msg            |
+----------------+
| this is test   |
| this is test 2 |
+----------------+

现在您可以看到重复项,可以确认并使用临时表中的数据删除它们。

现在你可以启用触发器。

是的,这是一个很长的路,也许有点慢,但这是另一种选择!