Click here to Skip to main content
12,350,035 members (22,108 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: SQL MySQL
I have two tables one is account table and the another is missinginfo table.

I want to insert the id column from the account table to the missing info table..


here is my query

insert into missinginfo ( mfname,mlname,id) values ('john','wilson', 'select id from account where username=abc@abc.com')

Query gives no error but the id column give wrong result i.e 0 ..

where is the problem..
Posted 10-Jan-14 2:01am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

insert into missinginfo ( mfname,mlname,id) values ('john','wilson', (select id from account where username='abc@abc.com'))
  Permalink  
v2
Comments
idle63 10-Jan-14 11:51am
   
The correct answer. 5 from my side.
KARTHIK Bangalore 10-Jan-14 12:07pm
   
Thanks a lot:)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Your insert query is wrong.
Check below tsql query.

insert into missinginfo ( mfname,mlname,id) select 'john','wilson',id from account where username='abc@abc.com'
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Remove the single quotes from the id-sql

like this
insert into missinginfo ( mfname,mlname,id) values ('john','wilson', select id from account where username=abc@abc.com)


[EDIT]correction after reading solution #1 from KARTHIK[EDIT]
insert into missinginfo ( mfname,mlname,id) values ('john','wilson', (select id from account where username=abc@abc.com))


and not like
insert into missinginfo ( mfname,mlname,id) values ('john','wilson', 'select id from account where username=abc@abc.com')
  Permalink  
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Use this

insert into missinginfo ( mfname,mlname,id) 
select 'john','wilson',id from account where username='abc@abc.com'

and if you want to insert multiple records then use this.
insert into missinginfo ( mfname,mlname,id) 
select 'john','wilson',id from account where username='abc@abc.com'
union all
select 'john2','wilson2',id from account where username='abc@abc.com'
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

You have wrong query there in id section in values.

Putting single quotes around your select statement makes it static string it wouldn't be executed.

Removing those single quote around your select statement will be executed.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 10 Jan 2014
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100