通过匹配链接表中的值来填充联结表
Posted
技术标签:
【中文标题】通过匹配链接表中的值来填充联结表【英文标题】:Fill junction table by matching values in linked tables 【发布时间】:2017-06-30 15:07:20 【问题描述】:我有一个数据库,其中包含几篇文章和相应的图像(位于不同的文件夹中)
产品图片有3个文件夹
在一个文件夹中只有技术图片,在其他营销图片中,在 第三个是用于打印目的的高质量图像
为了更快地检索产品信息或图像,我们使用“slugs”,文章编号从除数字之外的任何字符中删除
我在图像表上做了同样的事情,但因此有很多图像链接到一个产品(有时一个图像链接到不同的产品)我想将它们链接到连接表中
是否可以通过比较 tblArticles
.articleSlug
和 tblArticlepictures
.articlepictureSlug
中的值来使用 ID 填充联结表
我找到了这个资源,但它是基于一对多的关系
How do I insert into a table from another table by matching on values?
CREATE TABLE `tblArticles` (
`articleID` int(11) NOT NULL AUTO_INCREMENT,
`articleSlug` varchar(16) DEFAULT NULL,
`articleComment` varchar(1500) DEFAULT '',
PRIMARY KEY (`articleID`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `tblArticlepictures` (
`articlepictureID` int(11) NOT NULL AUTO_INCREMENT,
`articlepictureYear` int(4) DEFAULT NULL,
`articlepicturePath` varchar(255) NOT NULL,
`articlepictureSlug` varchar(45) DEFAULT NULL,
PRIMARY KEY (`articlepictureID`),
UNIQUE KEY `articlepicturePath_UNIQUE` (`articlepicturePath`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `tblRefArticleArticlepicture` (
`refArticleArticlepictureID` int(11) NOT NULL AUTO_INCREMENT,
`articleIDRef` int(11) NOT NULL,
`articlepictureIDRef` int(11) NOT NULL,
PRIMARY KEY (`refArticleArticlepictureID`),
KEY `fk_tblRefArticleArticlepicture_tblArticles1_idx` (`articleIDRef`),
KEY `fk_tblRefArticleArticlepicture_tblArticlepictures1_idx` (`articlepictureIDRef`),
CONSTRAINT `fk_tblRefArticleArticlepicture_tblArticlepictures1`
FOREIGN KEY (`articlepictureIDRef`) REFERENCES `tblArticlepictures` (`articlepictureID`)
ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_tblRefArticleArticlepicture_tblArticles1`
FOREIGN KEY (`articleIDRef`) REFERENCES `tblArticles` (`articleID`)
ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+-------------------+-------------------+-------------------+-----------------------+
| Picture | Folder | Article | Slug (in both tables) |
+-------------------+-------------------+-------------------+-----------------------+
| APS-4216_wr.jpg | W | APS-4216T | 4216 |
+-------------------+-------------------+-------------------+-----------------------+
| APS-9669 .jpg | W | APS-9669 | 9669 |
+-------------------+-------------------+-------------------+-----------------------+
| APS-2719NC.jpg | W | APS-2719 | 2719 |
+-------------------+-------------------+-------------------+-----------------------+
| EAP-3277T.jpg | W | EAP-3277 | 3277 |
+-------------------+-------------------+-------------------+-----------------------+
| EAP-8717T_1.jpg | W | EAP-8717Z | 8717 |
+-------------------+-------------------+-------------------+-----------------------+
| EAP-4530T.jpg | W | EAP-4530A | 4530 |
+-------------------+-------------------+-------------------+-----------------------+
| ----------------- | ----------------- | ----------------- | ----------------- |
+-------------------+-------------------+-------------------+-----------------------+
| APS-4216 t.jpg | P | APS-4216T | 4216 |
+-------------------+-------------------+-------------------+-----------------------+
| APS-9669_wr.jpg | P | APS-9669 | 9669 |
+-------------------+-------------------+-------------------+-----------------------+
| APS-2719 .jpg | P | APS-2719 | 2719 |
+-------------------+-------------------+-------------------+-----------------------+
| EAP-3277NC.jpg | P | EAP-3277 | 3277 |
+-------------------+-------------------+-------------------+-----------------------+
| EAP-8717T.jpg | P | EAP-8717Z | 8717 |
+-------------------+-------------------+-------------------+-----------------------+
| EAP-4530T_1.jpg | P | EAP-4530A | 4530 |
+-------------------+-------------------+-------------------+-----------------------+
【问题讨论】:
【参考方案1】:insert into tblRefArticleArticlepicture (articleIDRef, articlepictureIDRef)
select articleID as articleIDRef, articlepictureID as articlepictureIDRef
from tblArticles a
join tblArticlepictures p
ON a.articleSlug = p.articlepictureSlug
where a.articleSlug =p.articlepictureSlug
【讨论】:
以上是关于通过匹配链接表中的值来填充联结表的主要内容,如果未能解决你的问题,请参考以下文章