mysql - How to copy records to a table, but not if they are already there -
i have 3 tables use filing mechanism training documents, document table (a30), training manual table, stores titles , ids of different training manuals (a36trnman), , table store id's of documents associated different manuals (a31).
in application, allow user copy training manual, along associated documents , make own. however, run problem because different training manuals may include same documents, , there duplicates of document in training manual(which may have modified, , want preserved). so, need duplicate training manual query checks see if document in manual being copied exists user, , if does, leave in place, , create new record in table a31
associates existing (old) document training manual has been added.
my documents
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 ;
my current query 1. copies training manual, 2. stores training manual id, 3. copies list of associated document id's table a31, 4. copies documents table a30. how have query check existing duplicates in a30 , leave them be, still add entry a31 reference new training manual?
set @userid = <code type="user" />; create temporary table `a1t` engine=memory select * `a36trnman` `unik` = '<input name="unik" hidden="y" />'; update a1t set `unik` = null; insert `a36trnman` (select * `a1t`); drop table `a1t`; set @lastid := last_insert_id(); create temporary table `a1t` engine=memory select * `a31` `trnmannum` = '<input name="unik" hidden="y" />'; update a1t set `pntryid` = null, `trnmannum` = @lastid; insert `a31` (select * `a1t`); drop table `a1t`; create temporary table `a30t` engine=myisam select docname, docfile, docfilepdf, ctgry, subctgry, id, creator, smmry `a30` left join `a31` on a30.id = a31.pdocid `trnmannum` = @lastid; update a30t set `id` = null, `creator` = @userid; insert `a30` (select * `a30t`); drop table `a30t`;
don't think can current schema
ideal way add column documents table , populate uniqueid. change document links table use instead of auto inc id.
then insert documents weren't in there based on new identifier, never change, auto increment id must.
the links table work you'd using new immutable key.
so baically documents have 2 unique identifiers, 1 auto inc , 1 mysql's equivalent of guid.
Comments
Post a Comment