Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL INSERT SELECT problem

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?

like image 364
gtfx Avatar asked Sep 07 '09 11:09

gtfx


1 Answers

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
like image 77
Quassnoi Avatar answered Sep 22 '22 03:09

Quassnoi