简体   繁体   English

插入时的唯一记录mysql

[英]Unique record mysql on inserts

I want to implement a new system for referal/promotions. 我想实施一个新的推荐/促销系统。 my new system where when a user register a specific source is assig to him. 我的新系统,当用户注册一个特定的来源时,他就会得到他的帮助。 A source can be anything from referal (clients domain) and a promotion code (eg: 50% off). 来源可以是来自referal(客户域)和促销代码(例如:50%折扣)的任何内容。 This source also have a date (ymd). 此来源还有一个日期(ymd)。 Each source id is unique and multiple users can have the same source id 每个源ID都是唯一的,多个用户可以拥有相同的源ID

For example: 例如:

source id : 1 = www.domain1.com / 2011-11-20 / (empty referal code) 
source id : 2 = www.domain1.com / 2011-11-20 / referalcode1
source id : 3 = www.domain2.com / 2011-11-20 / referalcode1
source id : 4 = www.domain2.com / 2011-11-20 / referalcode2

The referal code can be mixed from client1 to client 2 可以将referal代码从client1混合到客户端2

How can i make sure when someone register (its free registration and we have over 1000 per hours now) we dont have duplicate records and risk the mysql to generate errors? 我如何确定当有人注册(它的免费注册,我们现在每小时超过1000)我们没有重复记录并冒险mysql产生错误?

From mysql page: mysql页面:

The main idea is to insert records using the ignore statement. 主要思想是使用ignore语句插入记录。 If you use the IGNORE keyword, errors that occur while executing the INSERT statement are treated as warnings instead. 如果使用IGNORE关键字,则执行INSERT语句时发生的错误将被视为警告。 For example, without IGNORE, a row that duplicates an existing UNIQUE index or PRIMARY KEY value in the table causes a duplicate-key error and the statement is aborted. 例如,如果没有IGNORE,则复制表中现有UNIQUE索引或PRIMARY KEY值的行会导致重复键错误,并且语句将中止。 With IGNORE, the row still is not inserted, but no error is issued. 使用IGNORE时,仍未插入行,但未发出错误。 Then retrieve the last ID using mysql_insert_id 然后使用mysql_insert_id检索最后一个ID

If the record does not exists, it will insert it and the last ID will be returned. 如果记录不存在,它将插入它并返回最后一个ID。

Now, if there's not record returned, you can do your select. 现在,如果没有返回记录,您可以进行选择。 This means a record already exists, so simply use it. 这意味着记录已经存在,所以只需使用它即可。

Example: 例:

// make sure $id is safe (preventing sql injections)
$sql = "INSERT IGNORE INTO yourtable SET `field1` = 'value1' ... "; 
mysql_query($sql);

if(mysql_affected_rows() == 1) { // no record found and then the inserts worked
  $id = mysql_insert_id(); // id from the primary key
  // add to cache
}
else {
  // you can also cache them when they were inserted (faster to run than a select statement)
  $id = retreiveFromCache($field1, $field2 /* etc...*/);
  if(!$id) { // no record found in cache
    // now the select can be done using the fields received
    // make sure your use the right index otherwise it can be slow
    $sql = "SELECT id FROM promotions_referrals WHERE `field1` = 'value1' ... "; // 
    $query = mysql_query($sql);
    $row = mysql_fetch_assoc($query);
    $id = $row['id'];
    // add to cache
  }
}

At this point you will have $id assigned and you are sure there's no duplicate/mysql errors. 此时您将分配$id并且您确定没有重复/ mysql错误。

Note: if a domain belongs to one client, use the client id instead of using the domain. 注意:如果域属于一个客户端,请使用客户端ID而不是使用域。 This way you will use INT as index which is faster than VARCHAR. 这样,您将使用INT作为索引,该索引比VARCHAR快。 Same idea for referral code. 推荐代码的想法相同。

You should define your column in MySQL as INTEGER AUTO_INCREMENT PRIMARY KEY . 您应该在MySQL中将列定义为INTEGER AUTO_INCREMENT PRIMARY KEY

When you do inserts, don't specify that column: 执行插入时,请不要指定该列:

INSERT INTO referral (domain, date, referralcode) VALUES (?,?,?)

If you need the ID you just generated, you can use the function mysql_insert_id 如果您需要刚刚生成的ID,可以使用函数mysql_insert_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM