我需要从 mem_count 等于 2 的表中获取数据,然后在最后一个 unique_id 之后将该数据插入到另一个表中。
这是我的代码:
$mem_count2=mysql_query("SELECT mem_count,mem_id FROM member_status WHERE mem_count = 2 order by mem_id ASC");
if(mysql_num_rows($mem_count2 )==0){
die(mysql_error());
}
else{
$start_value=00;
// $start_value dynamically comes from other table which i am not mentioning here
// $start_value can starts from any number For Eg. 2, 5.
while ($row=mysql_fetch_array($mem_count2)) {
$uniq_code="PHR-" . str_pad((int) $start_value, 2, "0", STR_PAD_LEFT);
//if mem_id already inserted then use IGNORE in INSERT i.e INSERT IGNORE
$cql = "INSERT IGNORE INTO member_code (mem_id, temp_code,unique_code) values ('".$row['mem_id']."','".$row['mem_count']."','".$uniq_code."')";
mysql_query($cql) or exit(mysql_error());
$start_value++;
}
}
它运行顺利,但有时我得到这个输出:
+------------+-------------+
| mem_id | unique_code |
+------------+-------------+
| 1 | PHR-01 |
+------------+-------------+
| 5 | PHR-02 |
+------------+-------------+
| 3 | PHR-04 |
+------------+-------------+
INSERT IGNORE 查询中肯定有一些问题!我已将 mem_id 设为唯一。请纠正这个问题,因为我完全被它困住了!
member_code 结构
+------------+-------------+-----+
| mem_id | unique_code | Id |
+------------+-------------+-----+
| 1 | PHR-01 | 1 |
+------------+-------------+-----+
| 5 | PHR-02 | 5 |
+------------+-------------+-----+
| 3 | PHR-04 | 3 |
+------------+-------------+-----+