我在下面有一个表格:
+-----+-------+------+------------+ | 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);
那么它不应该插入
。我在插入该表时正在检查,但它不起作用,因为我同时检查了 2 insert
查询正在发生,我需要使用 UNIQUE key
约束来停止它.
最佳答案
您需要更改数据库架构。通过 RefId 添加具有唯一索引的表,在这里您将写入类型为“1”的记录。在你的 Yii 模型中,在 beforesave 方法中检查你的类型,如果是 1 写入添加的表,否则写入旧表。你需要改变 beforefind 方法
对不起我的英语不好
关于php - MySQL 中的重复数据停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32090658/