Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two tables,mycode and mycode1

The two tables contain same field CODE and img. I need to insert the table2 to table 1 two tables. Both of them contain the same code numbers in CODE field.

eg: table 1

CODE img

110 null

111 null

112 image3.jpg

table 2

CODE img
110 imag1.jpg

111 image2.jpg



I need the output as

table2

CODE img
110 imag1.jpg

111 image2.jpg
112 image3.jpg
Posted
Updated 16-Jan-15 18:29pm
v2

You asked:
Quote:
I need to insert the table2 to table 1

That should be an "update" operation since all the code's in table2 exist in table1.
SQL
update table1 set img=(select img from table2 where table1.code=code)
where exists (select * from table2 where table1.code=code);
select * from table1
 
Share this answer
 
v2
Comments
Member 11357862 17-Jan-15 3:19am    
Sir I hve executed the statement but it shows this error.
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
Peter Leow 17-Jan-15 3:29am    
It means that your table2 has duplicate values in code field, say more than one 112, is that the case?
Member 11357862 17-Jan-15 4:05am    
thank u sir for ur great help
SQL
INSERT INTO table2
SELECT * FROM table1 t1
WHERE t1.code NOT IN (select t2.code from table2 t2)
 
Share this answer
 

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