php - MySQL 中的重复数据停止

标签 php mysql database duplicate-data

我在下面有一个表格:

+-----+-------+------+------------+
| 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/

相关文章:

php - preg_match() : Unknown modifier php function

mysql - 只需将mysql数据库中的5位数字转换为日期

php - 如何扩大 MySQL LIKE 查询的范围?

sql - 使用 else/if 语句还是 SQL 查询更快?

sql - postgres 更改表并插入行概率

php - 如何在 cakephp 1.2.6 中回显最后一个查询

php - 什么会导致 %5B0%5D 添加到 url

php - 使用Jquery发布单个变量

Mysql:从多个表中获取数据

ruby-on-rails - rake 中止! ActiveRecord::Base:Class 的未定义方法 `migration_error='