首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >插入时复制MySQL

插入时复制MySQL
EN

Stack Overflow用户
提问于 2018-06-30 22:37:18
回答 1查看 55关注 0票数 0

第一张表

代码语言:javascript
复制
  $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
)";

第二个表

代码语言:javascript
复制
$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)
)";

内连接表

代码语言:javascript
复制
      $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";

我第一次使用这段代码是可行的,但是当我向第一个和第二个表添加新行时,内部连接表没有更新它,给了我键'PRIMARY‘的重复条目,我如何才能更新这个表,添加新的行,而不改变已经存在的行?

EN

回答 1

Stack Overflow用户

发布于 2018-07-01 00:15:34

我想你不需要第三张桌子。您可以使用简单的join轻松地获得唯一记录。

代码语言:javascript
复制
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

我建议两个人只用两张桌子,不要用第二张

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51115872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档