user3099298 user3099298 - 3 months ago 8
SQL Question

Insert INTO MSSQL table FROM another table did not work?

I created two tables,

Table 1,

mail_groups
:

mail_group_id | mail_group_name


Table 2,
mail_recipient_list
:

id | mail_group_id | recipient_id


To insert data to
mail_recipient_list
table. I wrote following SQL, but it did not work.

INSERT INTO mail_recipient_list (mail_group_id, recipient_id)
SELECT TOP 1 id FROM mail_groups WHERE mail_group_name='".$key."', '".$value."'


As this query I want to get
mail_group_id
from
mail_groups table
. I can get
recipient_id
from html form. $key is
mail_group_name
.

Can someone please advice me to fix this issue. Thank you..

Answer

Like I suggested on the comments, your query should be:

"INSERT INTO mail_recipient_list (mail_group_id, recipient_id) 
SELECT TOP 1 id, '".$value."' FROM mail_groups WHERE mail_group_name='".$key."'";
Comments