Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
how to insert one column value from another table and rest of columns values manually in single sql.

Thanks in advance.
Posted

You can do a INSERT ... SELECT

SQL
INSERT INTO tbl (name,address,zipcode) SELECT myname,'My manual entered address','5500' FROM nametbl WHERE age > 12


Inserting Data from Other Tables[^] or MySql INSERT ... SELECT Syntax[^]
 
Share this answer
 
v3
Comments
Rohit.Net100 8-Jun-11 12:00pm    
INSERT INTO tbl (name) SELECT myname FROM nametbl
is ok.but i also want to insert another column value (address) into tb1, which not exists in nametb1.
Kim Togo 8-Jun-11 12:11pm    
See updated answer. You can combine the SELECT statement as you like.
Assuming that 'manually' means 'manually specified fixed values', here's an example:
SQL
insert into DestinationTable (
    copy_column_1
,   copy_column_2
,   manual_column_1
,   manual_column_2)
   select
        copy_column_1
    ,   copy_column_2
    ,   'manual_value_1'
    ,   'manual_value_2'
    from SourceTable


Edit: fixed syntax error
 
Share this answer
 
v2
Comments
Rohit.Net100 8-Jun-11 12:03pm    
this will not work. it gives you error.
deepika.tella 8-Jun-11 14:20pm    
Try this....
insert into table2(column1) select column2 from table1;
Rohit.Net100 8-Jun-11 12:05pm    
sorry, it will work, just syntax error.
dasblinkenlight 8-Jun-11 12:12pm    
Fixed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900