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

there are four column in tb1 tb1c1,tb1c2,tb1c3,tb1c4

and three column in tb2

tb2c1,tb2c2,tb2c3

when i am inserting values into the tb1 then I want tb2c2 containt as value into the tb1c3 field

I though to do something like follows:

SQL
insert into tb1(tb1c1,tb1c2,tb1c3,tb1c4) values('1','2',select tb2c2 fromtb2 where tb1c1=1)


please help me in this query

What I have tried:

I have two tables tb1 and tb2

there are four column in tb1 tb1c1,tb1c2,tb1c3,tb1c4

and three column in tb2

tb2c1,tb2c2,tb2c3

when i am inserting values into the tb1 then I want tb2c2 containt as value into the tb1c3 field

I though to do something like follows:

SQL
insert into tb1(tb1c1,tb1c2,tb1c3,tb1c4) values('1','2',select tb2c2 fromtb2 where tb1c1=1)


please help me in this query
Posted
Updated 21-Apr-16 1:55am
v3
Comments
Herman<T>.Instance 21-Apr-16 7:38am    
try this:
insert into tb1(tb1c1,tb1c2,tb1c3,tb1c4) values('1','2',(select tb2c2 fromtb2 where tb1c1=1))
Kishor-KW 21-Apr-16 7:47am    
yep,it's working thanx
Kishor-KW 21-Apr-16 8:08am    
yes it is woking but it returns more than one value in my case it returns 5 values it may be vary from 5 to 200 now how to handle this?
Thanx for reply

1 solution

Digimanus gave you one way, personally I think that is a bad format to do it in. I suggest doing it similar but like this:
SQL
insert into tb1(tb1c1,tb1c2,tb1c3,tb1c4) 
SELECT '1','2', tb2c2, someValue
from tb2 
where tb1c1=1


Note you still need something for tblc4 but you didn't have anything in your code you posted.
 
Share this answer
 
Comments
Kishor-KW 21-Apr-16 8:29am    
its giving a error of syntax did you missed the values key word?
ZurdoDev 21-Apr-16 8:38am    
No, see http://dev.mysql.com/doc/refman/5.7/en/insert-select.html

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