-1

I am new to MySQL and learning it to my own. Actually I want to copy a column from a table into my existing table column! suppose that my existing table is:

enter image description here

where pid values are inserted by default! now i want to copy a column from another table using:

  INSERT INTO exist_tab(FirstLevel) SELECT some_col FROM another_table;

so that the values should come inside FirstLevel Column. but the problem is that the copies values come below the pid values in FirstLevel Column as:

enter image description here

see that the firstlevel comes below! what is wrong with it? I need the "H" value against 19 but i dont want to use wild cards just want to copy the new data against old column data thanks I am new to this kind a work please can somebody give me any idea how to do it please! thanks in advance

Java Nerd
  • 958
  • 3
  • 19
  • 51

2 Answers2

0

You are using INSERT statement here. INSERT will create a new record in the table. You have to use UPDATE for updating a particular column in the existing table like this:

UPDATE exist_tab 
SET FirstLevel = (SELECT some_col FROM another_table)

If you want any conditional update then you can use JOIN like this:

UPDATE exist_tab a
LEFT JOIN another_table b ON
a.pid = b.id
SET FirstLevel = a.some_col;
Abhishekh Gupta
  • 6,206
  • 4
  • 18
  • 46
0

INSERT and UPDATE is different Command to Perform Different Task.

INSERT :Insert New Record into the table

Update:Update Existing Record in table If Exist.

NOT SURE ABOUT IT:(i'm Not Familiar With MYSQL)

Update a set 
a.FirstLevel=b.some_col
from 
exist_tab a join another_table b on a.Id=b.Id

Or You can Try :

update exist_tab a set a.FirstLevel=
(select top 1 some_col from another_table where Id=a.Id)

EDIT2:

update exist_tab a set a.FirstLevel=
    (select top 1 some_col from another_table)

You Can Find Here.

Community
  • 1
  • 1
A_Sk
  • 4,532
  • 3
  • 27
  • 51