MySQL 插入时复制

MySQL Duplicate on Insert

第一个table

  $sql = "CREATE TABLE if not exists mainInfo (
sku varchar(20) primary key not null,
name varchar(20) not null,
price int(30) not null,
type int(2) not null
)";

第二个Table

$sql="CREATE TABLE if NOT EXISTS properties (
size int(9),
bWeight int(9),
fHeight int(9),
fWeight int(9),
fLenght int(9),
sku varchar(20) not null,
CONSTRAINT FK_mainInfoProperties FOREIGN KEY (sku) REFERENCES mainInfo(sku)
)";

内连接table

      $sql = "CREATE TABLE if NOT EXISTS allInfo (
        sku varchar(20) primary key not null,
        name varchar(20) not null,
        price int(30) not null,
        type int(2) not null,
        size int(9),
        bWeight int(9),
        fHeight int(9),
        fWeight int(9),
        fLenght int(9)
         )";

       $sql = "INSERT INTO allInfo (sku, name, price, type, size, bWeight, 
       fHeight, fWeight, fLenght)
        SELECT mainInfo.sku, name, price, type, size, bWeight, fHeight, 
       fWeight, fLenght
        FROM mainInfo INNER JOIN properties
        ON mainInfo.sku = properties.sku";

我第一次使用这段代码时它起作用了,但是当我向第一个和第二个 table 添加新行时,内部联接 table 没有更新它,为我提供了键的重复条目 'PRIMARY' 我如何更新此 table 添加新行但保留已经存在的行?

allInfo 中的 Suk 字段标记为外键 并添加新的主键如 allInfoId 以唯一标识记录

我认为你不需要第三个 table。您可以使用简单的连接轻松获得唯一记录。

Select m.sku ,a.bWeight, a.fHeight, a.fWeight , a.fLenght from mainfon join properties a on m.sku=a.suk where a.suk=your_id

我建议两个只用两个table,不要用第二个