我有一个包含多篇文章和相应图片的数据库(放在不同的文件夹中)
有3个包含产品图片的文件夹
在一个文件夹中只有技术图片,在其他营销图片中 第三种是用于印刷目的的高质量图像
为了更快地检索产品信息或图片,我们使用'slugs',文章编号从数字旁边的任何字符中删除
我在图像表上做了同样的事情,但因此有很多图像链接到一个产品(有时一个图像链接到不同的产品)我想在联结表中链接它们
是否可以根据比较tblArticles
。articleSlug
和tblArticlepictures
中的值来填充联结表中的ID。articlepictureSlug
我找到了这个资源,但它基于1对多关系
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 |
+-------------------+-------------------+-------------------+-----------------------+
答案 0 :(得分:0)
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