|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
copying a column within the same table
I need to copy a column which contains a list of integers to a new column. Can someone please tell me how I can do this using a query statement? Thanks in advance.
|
|
#2
|
|||
|
|||
|
Hi,
Assume tbl is a table and Old is the old column and New is the new column. If u want to copy the values from Old to New, then run the following MS SQL stmt. Quote:
__________________
Regards, Brightlight
|
|
#3
|
|||
|
|||
|
Quote:
OK, what if I wish to copy a column from table1 and insert it to table2 ? how can you do this in mysql or SQL command please??? |
|
#4
|
|||
|
|||
|
brightlight
I can already say thank you, as your post did help me big time. still please, if you can answer my previous question in the previous post..... please do. |
|
#5
|
|||
|
|||
|
If you just want to copy the column's data then just Select Into another table to hold it till you want to do whatever it is you want to do w/it...
Code:
select [origField] into newTable from origTable --origTable being the table that holds the orig column Else, if you want to do as BrightLight was getting @...Give this a shot... Code:
--table2 is the target table, table1 has the orig data to be copied update table2 inner join table1 on table2.[relatedField] = table1.[relatedField] set table2.[targetField] = table1.[origField] |
|
#6
|
|||
|
|||
|
i feel kind of ashamed to say i tried the script you propose.... and :$ it did not work .. maybe I did something wrong.
but are you sure ? |
|
#7
|
|||
|
|||
|
maybe, then again, it may be my code
Post it and lets see if we can't get you rolling. |
|
#8
|
|||
|
|||
|
Quote:
Let me tell you first what I did. I phpmyadmin exported table1, that is evo_blogs, changed its name in the dump to test_copy_column, removed the "blog_shortname" content, and pasted the modified dump into the SQL table. Thus test_copy_column is EXACTLY a copy of evo_blogs, except for the missing "blog_shortname" column content. Let us try to copy this from evo_blogs, table 1, to test_copy_column, table 2 (both tables within the same DB) table 1 `evo_blogs` table 2 `test_copy_column` column to be copied `blog_shortname` your code: PHP Code:
thus PHP Code:
======================================== result: PHP Code:
================================================================================ is it the apostrophe like things? , let us remove those and try: PHP Code:
================================================================================= result: PHP Code:
|
|
#9
|
|||
|
|||
|
I'm not familiar with PHP code but for the SQL query syntax its not necessary for the quotes and square brackets []. Check without the brackets and see what u get.
Quote:
|
|
#10
|
|||
|
|||
|
Quote:
true ..... and no error .... but the column was not copied anyway... Affected rows: 0 (Query took 0.0004 sec) SQL-query : [Edit] [Create PHP Code] UPDATE test_copy_column INNER JOIN evo_blogs ON test_copy_column.blog_shortname = evo_blogs.blog_shortname SET test_copy_column.blog_shortname = evo_blogs.blog_shortname |
|
#11
|
||||
|
||||
|
Yes, definitely the columns will not be copied because from the syntax suggested by username=null the related field and the target field both should be different. In ur sql the related and the target field are the same.
when ur test_copy_column.blog_shortname is null then it is never going to satisfy the following condition Quote:
Use a common field such as id or something to satisfy the above condition then the columns will be copied. I'll give u a sample Quote:
|
|
#12
|
|||
|
|||
|
Hi
I filled all the rows of the targeted field with data, and the script still did not work I tried editing the type of column, and made test_copy_column.blog_shortname as Not Null, and TEXT....... still did not work. |
|
#13
|
|||
|
|||
|
can u give me the table structure of ur two tables and let me see what can be done.
|
|
#14
|
|||
|
|||
|
BrightLight is on top of it...to nail your solution you really have to have a fundemental understanding of how a inner join works. Go back to my first reply for a sec...well, here, just refer to this
...Code:
update table2 inner join table1 on table2.[relatedField] = table1.[relatedField] set table2.[targetField] = table1.[origField] In order to do a join on the 2 tables, you need to define the relation to join on. I don't know your exact structure so my "code" is just skeleton code for you to follow. Refer to this visual, it may help... Code:
Table1 Table2 ------ ------ relatedField--> relatedField origField targetField ...you see there are 2 columns in each table. The 2 tables are joined w/the related fields, once that is established, the origField from Table1 can then be copied (really doing an Update) to Table2. You're trying to join AND do the update all w/the same columns, possible, but w/your example it's not gonna work. Oh ya, fyi...if you're using MySQL there is a seperate forum for MySQL, they may be able to find syntax errors better. One more thing...answer me this if you don't mind, are you just copying that 1 column from the table into another table?...is that you're objective? If so, my first snip of code (Select INTO) will work as well...maybe give it a shot? |