本文介绍了MySQL中重复数据停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张表:

 
+ ----- + ------- + ---- - + ------------ +
| ID | RefID |类型| EventTime |
+ ----- + ------- + ------ + ------------ +
| 101 | 228 | 1 | 1437195633 |
| 102 | 228 | 5 | 1437195633 |
| 103 | 228 | 1 | 1437195633 |
| 104 | 228 | 1 | 1437195442 |
| 105 | 228 | 1 | 1437195442 |
| 106 | 228 | 5 | 1437195442 |
| 107 | 228 | 1 | 1437165634 |
| 108 | 228 | 5 | 1437165442 |
| 109 | 228 | 1 | 1437165634 |
| 110 | 228 | 5 | 1437165442 |
+ ----- + ------- + ------ + ------------ +

因此,我想停止插入重复数据,基于列 RefID,Type,EventTime 仅当值 Type = 1



在上表中 ID 对重复(101,103),(104,105),(107,109)



如果现在我将插入另外一个数据:

  INSERT INTO表VALUES('',228,1,1437165634); 

然后不应该 insert 。我正在检查插入到该表,但是这不工作,因为我已经检查同时 2插入查询正在发生,我需要停止它使用 UNIQUE键约束。

解决方案

我已经通过使用触发器如下解决了: p>

数组触发器

 分隔符$$ 

drop触发器如果存在stop_duplicate $$
创建触发器stop_duplicate之前插入表
为每一行
begin
set @found:= false;
如果new.Type = 1 then
SELECT
TRUE
INTO @found FROM

WHERE
RefID = new.RefID AND EventTime = new.EventTime AND Type = new.Type;

if @found then
signal sqlstate'23000'set message_text ='CUSTOM_MSG_DUPLICATE';
end if;
end if;
end $$
分隔符;

Yii模型代码



pre> public function save($ runValidation = true,$ attributes = null){
Yii :: log(__ METHOD__。:: Start,'info');
try {
return parent :: save();
} catch(Exception $ e){
$ errorInfo = $ e instanceof PDOException? $ e-> errorInfo:null;
$ message = $ e-> getMessage();
Yii :: log(__ METHOD__。:errorcode:{$ e-> getCode()},errormessage:{$ message},'info');
//为索引
添加处理重复条目的问题if((int)$ e-> getCode()=== 23000&& strpos($ message,'CUSTOM_MSG_DUPLICATE')!= = false){
return false;
}
抛出新的CDbException(Yii :: t('yii','CDbCommand无法执行SQL语句:{error}',array('{error}'=> $ message)) ,(int)$ e-> getCode(),$ errorInfo);
}
}


I have a table below :

+-----+-------+------+------------+
| ID  | RefID | Type | EventTime  |
+-----+-------+------+------------+
| 101 |   228 |    1 | 1437195633 |
| 102 |   228 |    5 | 1437195633 |
| 103 |   228 |    1 | 1437195633 |
| 104 |   228 |    1 | 1437195442 |
| 105 |   228 |    1 | 1437195442 |
| 106 |   228 |    5 | 1437195442 |
| 107 |   228 |    1 | 1437165634 |
| 108 |   228 |    5 | 1437165442 |
| 109 |   228 |    1 | 1437165634 |
| 110 |   228 |    5 | 1437165442 |
+-----+-------+------+------------+

In that I want to stop inserting duplicate data based on the columns RefID,Type,EventTime only when value of Type = 1.

In the above table ID pair is duplicate (101,103), (104,105), (107,109).

If now I will insert another data say :

INSERT INTO table VALUES('',228,1,1437165634);

Then it should not insert. I am checking while inserting into that table but that is not working as I have checked at the same time 2 insert query is happening, I need to stop it using UNIQUE key constraints.

解决方案

I have solved it by using trigger as below :

DB Trigger

delimiter $$

drop trigger if exists stop_duplicate $$
create trigger stop_duplicate before insert on table
for each row
begin
  set @found := false;
  if new.Type = 1 then
SELECT
        TRUE
    INTO @found FROM
        table
    WHERE
        RefID = new.RefID AND EventTime= new.EventTime AND Type= new.Type;

      if @found then
        signal sqlstate '23000' set message_text = 'CUSTOM_MSG_DUPLICATE';
        end if;
end if;
end   $$
delimiter ;

Yii Model Code

public function save($runValidation = true, $attributes = null) {
    Yii::log(__METHOD__.":: Start ", 'info');
    try {
        return parent::save();
    } catch (Exception $e) {
        $errorInfo = $e instanceof PDOException ? $e->errorInfo : null;
        $message = $e->getMessage();
        Yii::log(__METHOD__ . ": errorcode :{$e->getCode()}, errormessage:{$message} ", 'info');
        // Added for handling duplicate entry issue for index
        if ((int) $e->getCode() === 23000 && strpos($message, 'CUSTOM_MSG_DUPLICATE') !== false) {
            return false;
        }
        throw new CDbException(Yii::t('yii', 'CDbCommand failed to execute the SQL statement: {error}', array('{error}' => $message)), (int) $e->getCode(), $errorInfo);
    }
}

这篇关于MySQL中重复数据停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 21:01