PDA

View Full Version : fill missing id-s into detail table


BubikolRamios
02-20-2010, 05:33 PM
Is there any way except using temp table to fill missing 2 and 6 into detail ?
like something like: insert .. join

master

id
1
2
3
4
5
6


detail

id
1
3
4
5


example how I did it in 3 steps

drop VIEW if exists tmp_view;

CREATE VIEW tmp_view AS
select g.id_galery,'si' as cc2 from master g left join detail p
on g.id_galery = p.id_galery
where p.id_galery is null;

insert into detail select id_galery,cc2,'','0','miran',null from tmp_view

Old Pedant
02-20-2010, 08:21 PM
I don't see any temp table there.

You just created a view and then used it. There's no temp table created when using a view.

But I don't see why you need the view.

insert into detail
select T.id_galery, T.cc2, '', '0', 'miran', null
FROM
( select g.id_galery,'si' as cc2 from master g left join detail p
on g.id_galery = p.id_galery
where p.id_galery is null
) AS T

Maybe that's too complex for poor little MySQL? But worth a try.

But looks to me like you could also try:

INSERT INTO detail
SELECT id_galery, 'si', '', '0', 'miran', null
FROM master
WHERE id_galery NOT IN (
SELECT id_galery FROM detail )

Again, unless that's too complex for MySQL.