本文介绍了MySQL:如何插入多个具有相同ID自动递增的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有1列的表,其中存储着ID auto_increment并带有以下指令:

I have a table with 1 column that stores the ID auto_increment and with that instruction:

$query = "SELECT first_data, second_data, third_data
            FROM table_data
                WHERE condition = 'value_condition'";
$result = mysql_query($query) or die(mysql_error());

$no_of_rows = mysql_num_rows($result);

    if ($no_of_rows > 0) {
        while($row = mysql_fetch_assoc($result)) {
            $valueone = $row['first_data'];
            $valuetwo = $row['second_data'];
            $valuethree = $row['third_data'];

            $queryTwo = "INSERT INTO historial_ventas (reg1, reg2, reg3)
                        VALUES('$ivalueone','$valuetwo','$valuethree')";
            $resultTwol = mysql_query($queryTwo) or die(mysql_error());
        }
    } else {
        return false;
}

信息存储如下:

ID(auto_increment)  reg1      reg2      reg3
______________________________________________
1                   value1    value2    value3
2                   value1    value2    value3
3                   value1    value2    value3
4                   value1    value2    value3

但是我希望将记录保存在while循环中,并使用与在线购买时相同的ID来存储,即,您可以通过以下方式保存产品及其功能,但要购买具有相同的ID:

But I want the records maintained in the while loop, stored with the same ID as in a purchase online, ie you save the products and their features but with the same ID to buy, as this way:

ID(auto_increment)  reg1      reg2      reg3
______________________________________________
1                   value1    value2    value3
1                   value1    value2    value3
1                   value1    value2    value3
1                   value1    value2    value3

推荐答案

归一化方法是使用如下联合表:

The normalized way is to use a joint table like this:

PurchaseId ItemId 均设置为自动递增.主键的目的是唯一地标识每一行.

Both PurchaseId and ItemId are set to auto-increment. The purpose of a Primary Key is to uniquely identify each row.

阅读数据库规范化,了解如何设计有效的数据库.

Read up on Database Normalization on how you can design your database to be effective.

这篇关于MySQL:如何插入多个具有相同ID自动递增的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 00:11