I have two tables which share primary keys. I designed poorly, and it turns out that I need to ensure that every record in a1 has a corresponding record in a4
Query:
SELECT a1.id1, a4.id4
FROM `a1`
LEFT JOIN `a4` ON a1.id1 = a4.id4
Results:
a1.id1.............a4.id4
00000001 ......NULL
00000002 ......NULL
00001001 ......00001001
00001002 ......00001002
What is the best way to INSERT rows in a4 with a corresponding key to a1? In the example above, I need to insert the records 00000001 and 00000002 into a4. 00001001 and 00001002 should be left alone because they already exist in both a1 and a4
Database schema:
CREATE TABLE `a1` (
`id1` int(8) unsigned zerofill NOT NULL auto_increment,
`Shrt_Desc` varchar(200) default NULL,
`ptype` int(5) NOT NULL,
`userid` tinyint(5) NOT NULL,
`submit_id` int(11) NOT NULL,
`submit_time` int(11) NOT NULL,
`update_id` int(11) NOT NULL,
`update_time` int(11) NOT NULL,
`pub` tinyint(1) default '1',
`plate` tinyint(1) NOT NULL,
`item` varchar(11) default NULL,
PRIMARY KEY (`id1`),
KEY `fb_groupbyorder_Shrt_Desc_INDEX` (`Shrt_Desc`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=124106 ;
CREATE TABLE `a4` (
`id4` int(8) unsigned zerofill NOT NULL,
`Water` decimal(10,2) default NULL,
PRIMARY KEY (`id4`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
http://sqlfiddle.com/#!2/91dc1/8