mysql - create missing rows in left joined tables -


i have 2 tables share primary keys. designed poorly, , turns out need ensure every record in a1 has corresponding record in a4

query:

select a1.id1, a4.id4  `a1`  left join  `a4` on a1.id1 = a4.id4 

results:

a1.id1.............a4.id4   00000001    ......null   00000002    ......null   00001001    ......00001001   00001002    ......00001002   

what best way insert rows in a4 corresponding key a1? in example above, need insert records 00000001 , 00000002 a4. 00001001 , 00001002 should left alone because exist in both a1 , 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; 

insert a4 (id4) select a1.id1  `a1`  left join  `a4` on a1.id1 = a4.id4 a4.id4 null 

http://sqlfiddle.com/#!2/91dc1/8


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -