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 > INSERT and subquery - cant figure this out

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-05-12, 12:25
tbobker tbobker is offline
Registered User
 
Join Date: Feb 2012
Posts: 1
INSERT and subquery - cant figure this out

I have found many examples and information around this but still cant get this to work. What I am trying to do is insert into a table, but query two other tables to get the foreign keys so I need to use a subquery.

I have 3 tables: user, hair and city. Hair contains different colours and city are different cities. The below example i am trying to use does not try and insert city (its been added manually for now but will need to be another subquery) and have tried to insert hair colour.

This is what I have so far:

INSERT INTO user (lid,random,email,phone,name,city,hair)
SELECT '','19293642446','someone@whereareyou.com','078953 33555','Someone Somewhere','london',hair.id
FROM hair
WHERE hid.colur = 'red'

There is no error just doesnt insert a new row.
Reply With Quote
  #2 (permalink)  
Old 02-05-12, 14:12
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by tbobker View Post
There is no error ...
sorry, there is so

where are you running this query from? php?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 02-06-12, 03:34
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
If your SELECT statement above returns a result, then you must have an error during the insert statement. If the SELECT statement fails (due to colur = 'red' not existing) then you will need to look at adding this in before attempting this insert.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
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