insert unrelated values from multiple mysql tables -
i have app reads multiple mysql tables, i'd put data 1 table. thing is, these tables have no linking fields... app sequentially processes rows across 3 tables, hope correct rows lined in each table (i.e. row1 in table1 applicable row1 in table 2 , table3, , on)
my tables follows:
table1: name,surname,id,dob
table2: address,town,state
table3: password
what want : table4: name,surname,id,dob,address,town,state,password
i have created table4 , i'm trying insert values select query... i've tried ...
select t1.name, t1.surname, t1.id, t1.dob, t2.address, t2.town, t2.state, t3.password table1 t1,table2 t2, table3 t3;
...but gives me duplicate rows cos there no clause. , since there's no linking fields, can't use join statement, right? i'm not experienced sql, please help!
well, officialy you're fucked up. there no first or last row in rdbms unless use order by
clause. that's manual states. if issue
select * your_table;
you can not sure result in same order rows inserted or in same order every time issue statement @ all.
in practice on other hand, of time same result , of time in same order rows inserted.
what can do, is, first slap 1 didn't think of putting column in each table determines sort order (in future use either auto_increment column or timestamp column holds date , time of insertion or whatever suits needs) , second, (but if have no other choice, said it's unreliable) can emulate row number on can join.
select * ( select table1.*, @rn1 := @rn1 + 1 row_number table1, (select @rn1 := 0) v ) left join ( select table2.*, @rn2 := @rn2 + 1 row_number table2, (select @rn2 := 0) v ) b on a.row_number = b.row_number left join ( select table3.*, @rn3 := @rn3 + 1 row_number table3, (select @rn3 := 0) v ) c on a.row_number = c.row_number
Comments
Post a Comment