Bernd
Bernd

Reputation: 165

Insert into with select (prepared statement)

I want this query:

INSERT INTO
    users_groups (user_id, group_id)
VALUES
    (?, (SELECT group_id FROM groups WHERE group_name = ?))

tables:

users_groups:
| user_id | group_id |

groups
| group_id | group_name |

How can I get this working?

Upvotes: 4

Views: 2563

Answers (2)

cybork
cybork

Reputation: 587

Leave 'VALUES' out of your statement. What do you mean with the '?'; parameters? Try the script beneath. Does that serve your purpose?

 INSERT INTO
 users_groups (group_id)
 SELECT group_id FROM groups WHERE group_name = [YOUR SELECTION]

Something like that (?). Or what do you mean exactly what you want to work? (PS: due to not enough rep points I can't add comments, so I have to ask the question here; I'll delete it later if answered)

Upvotes: 1

Praveen
Praveen

Reputation: 9345

Try this;

INSERT INTO
    users_groups (user_id, group_id)
VALUES
    (SELECT ?, group_id FROM groups WHERE group_name = ?)

Upvotes: 1

Related Questions