This is a discussion on how I copy a column betwen tables mysql? within the MySQL forums, part of the Database Server Software category; --> I have "tableA" with column "a" and "tableB" with column "b". Column "a"has new material so i need to ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| I have "tableA" with column "a" and "tableB" with column "b". Column "a"has new material so i need to copy the info from tableA column "a" in to tableB column "b", tableB is empty so i can't say where tableA.id = TableB.id waht can i do? please help please help. give examples i am a newbe this is mysql thank you |
| |||
| fhelik wrote: > I have "tableA" with column "a" and "tableB" with column "b". > > Column "a"has new material so i need to copy the info from tableA > column "a" in to tableB column "b", tableB is empty so i can't say > where tableA.id = TableB.id > waht can i do? please help > please help. give examples i am a newbe > > this is mysql > > thank you Hi one approach.. The table is empty instead you can create a new one. ex: mysql> create table a (aa integer); Query OK, 0 rows affected (0.12 sec) mysql> insert into a values(1); Query OK, 1 row affected (0.00 sec) mysql> create table b as select * from a; Query OK, 1 row affected (0.07 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> select * from b -> ; +------+ | aa | +------+ | 1 | +------+ 1 row in set (0.00 sec) If you want a different name for the table "b" try this mysql> create table c as select aa bb from a; Query OK, 1 row affected (0.06 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> select * from c; +------+ | bb | +------+ | 1 | +------+ 1 row in set (0.00 sec) Thanks Devi |
| |||
| Hi, Your solution is to create a new table and copy something from old table. how about this case: table a and table b both has column c1,c2 and c3, now I need copy record from a to b when I find one which has c1 value is "abc" in table a. I can do it like this: run "select c1,c2,c3 from a where c1=abc" firstly, get the values, then "insert to b (`c1`,`c2`,`c3`) values ($values)". but, in the future, the table structure maybe will change(add one column or delete one column), then this code needs be changed as well. Is there a way to ignore the exact table structure? just copy the total records to another table which has same structure? then we don't need change code when table structure changed. Thanks. Devi wrote: > fhelik wrote: > > I have "tableA" with column "a" and "tableB" with column "b". > > > > Column "a"has new material so i need to copy the info from tableA > > column "a" in to tableB column "b", tableB is empty so i can't say > > where tableA.id = TableB.id > > waht can i do? please help > > please help. give examples i am a newbe > > > > this is mysql > > > > thank you > > > Hi > > one approach.. The table is empty instead you can create a new one. > ex: > mysql> create table a (aa integer); > Query OK, 0 rows affected (0.12 sec) > > mysql> insert into a values(1); > Query OK, 1 row affected (0.00 sec) > > mysql> create table b as select * from a; > Query OK, 1 row affected (0.07 sec) > Records: 1 Duplicates: 0 Warnings: 0 > > mysql> select * from b > -> ; > +------+ > | aa | > +------+ > | 1 | > +------+ > 1 row in set (0.00 sec) > > If you want a different name for the table "b" > try this > > mysql> create table c as select aa bb from a; > Query OK, 1 row affected (0.06 sec) > Records: 1 Duplicates: 0 Warnings: 0 > > mysql> select * from c; > +------+ > | bb | > +------+ > | 1 | > +------+ > 1 row in set (0.00 sec) > > > Thanks > Devi |
| |||
| In article <1159517331.621514.316000@h48g2000cwc.googlegroups .com>, Hans says... > Hi, > Your solution is to create a new table and copy something from old > table. how about this case: > table a and table b both has column c1,c2 and c3, now I need copy > record from a to b when I find one which has c1 value is "abc" in table > a. I can do it like this: > run "select c1,c2,c3 from a where c1=abc" firstly, get the values, then > "insert to b (`c1`,`c2`,`c3`) values ($values)". > but, in the future, the table structure maybe will change(add one > column or delete one column), then this code needs be changed as well. > Is there a way to ignore the exact table structure? just copy the total > records to another table which has same structure? then we don't need > change code when table structure changed. INSERT INTO b SELECT * FROM a WHERE c1 = abc; -- PleegWat Remove caps to reply |
| ||||
| this is a better solution PleegWat wrote: > In article <1159517331.621514.316000@h48g2000cwc.googlegroups .com>, Hans > says... > > Hi, > > Your solution is to create a new table and copy something from old > > table. how about this case: > > table a and table b both has column c1,c2 and c3, now I need copy > > record from a to b when I find one which has c1 value is "abc" in table > > a. I can do it like this: > > run "select c1,c2,c3 from a where c1=abc" firstly, get the values, then > > "insert to b (`c1`,`c2`,`c3`) values ($values)". > > but, in the future, the table structure maybe will change(add one > > column or delete one column), then this code needs be changed as well. > > Is there a way to ignore the exact table structure? just copy the total > > records to another table which has same structure? then we don't need > > change code when table structure changed. > > INSERT INTO b > SELECT * FROM a > WHERE c1 = abc; > -- > PleegWat > Remove caps to reply |
| Thread Tools | |
| Display Modes | |
|
|