views:

120

answers:

1

Hey all,

i have a little difficulty in understanding how to do some INSERT SELECT.

For instance i have two tables.

TABLE : users  

 id | name   | gender  
 1  | John   | m  
 2  | Mary   | f  

TABLE : website  

 fid | url             | id  
 1   | www.desilva.biz | 2  
 2   | gidhelp.com     | 4

Now let's say i want to add another query to the table website. I get two variables, lets say:

$user = John;
$site = "www.google.com";

i want to select the id of John from users table and insert it into website table in one statement.

How can i do it?

+6  A: 

Assuming your variables are already escaped properly and are not subject to SQL injection:

INSERT
INTO    website (url, fid)
SELECT  $site, id
FROM    users
WHERE   name = $user
Quassnoi
I'd quote at least $user so that it matched correctly on names that contain spaces.
Adriano Varoli Piazza
+1 deleted my own answer because it was the same...
Roee Adler
Thank you for a quick reply.
gtfx