1

I have 2 tables in my database (friends / guestbook).

I'm using the following query to select all the friends for a specific user.

SELECT FID FROM `friends` as F WHERE UID = 1

What I'd like to do is insert a row on the 'guestbook' table for each result that is returned from the query above.

INSERT INTO `guestbook` (`FID`, `UID`, `message`) VALUES ('FID SHOULD BE EQUAL TO FID FROM PREVIOUS QUERY', '1', 'message goes here!')

How can this be done?

Shadow
  • 33,525
  • 10
  • 51
  • 64
master00
  • 141
  • 2
  • 12

1 Answers1

1

Use insert . . . select:

INSERT INTO `guestbook` (`FID`, `UID`, `message`)
    SELECT FID, 1, 'message'
    FROM `friends` as F
    WHERE UID = 1;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786