我有 3 个表用作培训文档的归档机制,一个文档表 (a30),一个培训手册表,用于存储不同培训手册 (a36trnman) 的标题和 id,以及一个存储 id 的表与不同手册相关联的文件 (a31)。
在我的应用程序中,我允许用户复制培训手册及其相关文档并将其设为自己的。但是,我遇到了一个问题,因为不同的培训手册可能包含相同的文档,然后他们的培训手册中现在会有该文档的副本(他们可能已经修改,并且希望保留)。因此,我需要一个重复的培训手册查询,以检查该用户是否已经存在正在复制的手册中的文档,如果存在,则将其保留在原处,并在表a31
中创建与现有关联的新记录(旧)包含刚刚添加的培训手册的文件。
我的文件
CREATE TABLE `a30` (
`docname` varchar(250) NOT NULL,
`docfile` varchar(300) NOT NULL,
`docfilepdf` varchar(300) NOT NULL,
`ctgry` int(8) unsigned zerofill NOT NULL,
`subctgry` int(8) unsigned zerofill NOT NULL,
`id` int(8) unsigned zerofill NOT NULL auto_increment,
`creator` int(8) unsigned zerofill NOT NULL,
`smmry` varchar(60) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='haha!! training materials!' AUTO_INCREMENT=17 ;
CREATE TABLE `a31` (
`pntryid` int(8) unsigned zerofill NOT NULL auto_increment,
`pdocid` int(8) unsigned zerofill NOT NULL,
`trnmannum` int(8) unsigned zerofill NOT NULL,
PRIMARY KEY (`pntryid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='document pantry' AUTO_INCREMENT=43 ;
CREATE TABLE `a36trnman` (
`unik` int(8) NOT NULL auto_increment,
`user` int(8) NOT NULL,
`ctgryname` char(60) NOT NULL,
PRIMARY KEY (`unik`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=88 ;
我当前的查询是 1. 复制培训手册,2. 存储培训手册 id,3. 然后将相关文档 id 的列表复制到表 a31,4. 将文档复制到表 a30。如何对 a30 中的现有重复项进行查询检查并保留它们,但仍然参考新的培训手册向 a31 添加条目?
SET @userid = <code type="user" />;
CREATE TEMPORARY TABLE `a1t` ENGINE=MEMORY SELECT * FROM `a36trnman` WHERE `unik` = '<input name="unik" hidden="y" />';
UPDATE a1t SET `unik` = NULL;
INSERT INTO `a36trnman` (SELECT * FROM `a1t`);
DROP TABLE `a1t`;
SET @lastid := LAST_INSERT_ID();
CREATE TEMPORARY TABLE `a1t` ENGINE=MEMORY SELECT * FROM `a31` WHERE `trnmannum` = '<input name="unik" hidden="y" />';
UPDATE a1t SET `pntryid` = NULL, `trnmannum` = @lastid;
INSERT INTO `a31` (SELECT * FROM `a1t`);
DROP TABLE `a1t`;
CREATE TEMPORARY TABLE `a30t` ENGINE=MyISAM SELECT docname, docfile, docfilepdf, ctgry, subctgry, id, creator, smmry FROM `a30` LEFT JOIN `a31` ON a30.id = a31.pdocid WHERE `trnmannum` = @lastid;
UPDATE a30t SET `id` = null, `creator` = @userid;
INSERT INTO `a30` (SELECT * FROM `a30t`);
DROP TABLE `a30t`;