Unix Technical Forum

how I copy a column betwen tables mysql?

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 ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 09:16 AM
fhelik
 
Posts: n/a
Default how I copy a column betwen tables mysql?

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 09:16 AM
Devi
 
Posts: n/a
Default Re: how I copy a column betwen tables mysql?


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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 09:16 AM
Hans
 
Posts: n/a
Default Re: how I copy a column betwen tables mysql?

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


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 09:16 AM
PleegWat
 
Posts: n/a
Default Re: how I copy a column betwen tables mysql?

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 09:17 AM
Devi
 
Posts: n/a
Default Re: how I copy a column betwen tables mysql?

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


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:19 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com