If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Subquery at Insert

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-31-06, 01:02
Stephensaw Stephensaw is offline
Registered User
 
Join Date: Oct 2006
Posts: 11
Subquery at Insert

I'm going to insert new record to table 1, while one of the value I would like to do some select from other table to get the value, here is what my statement looks like:

Code:
Insert Into Table1(col1,col2,col3)values('1','a',x);
Where the value x I would like to do Select which looks like this:

Code:
Select col6 From Table2 Where col1='abc';
How do I join these two query so that it become one single subquery? THanks.
Reply With Quote
  #2 (permalink)  
Old 10-31-06, 07:19
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
insert
  into Table1
     ( col1
     , col2
     , col3 )
select '1'
     , 'a'
     , col6
  from Table2
 where col1 = 'abc'
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 11-01-06, 01:54
Stephensaw Stephensaw is offline
Registered User
 
Join Date: Oct 2006
Posts: 11
Sorry if I'm not stated clearly, the value '1' and value 'a' at the Insert statement I would like to define the value myself, whereby the value x is a value that I get from Select statement, where I select only one column from Table2 where the column1's value at Table2 will be 'abc'...
Reply With Quote
  #4 (permalink)  
Old 11-01-06, 02:29
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
This is exactly what r937's statement does. You will need to replace '1' and 'a' with the value that you "define yourself" obviously.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On