3

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?

Bernd
  • 155
  • 8
  • 1
    Embed the value as part of the select statement, you can't mix concepts of a variable and select like you tried. `(SELECT ?, group_id FROM groups WHERE group_name = ?)` – xQbert Sep 08 '15 at 16:09

2 Answers2

1

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)

cybork
  • 569
  • 2
  • 5
  • 24
1

Try this;

INSERT INTO
    users_groups (user_id, group_id)
VALUES
    (SELECT ?, group_id FROM groups WHERE group_name = ?)
Praveen
  • 8,945
  • 4
  • 31
  • 49