1

I'm not proficient with SQL and Ive been looking for a solution to which is probably a simple one for those that know what they're doing.

I have a two column table with user_id and group_id. It stores all the groups a particular user is a part of.

i.e.

user_id, group_id 2 4 2 6 2 7 3 4 4 6 4 7 

You get the idea.

All I want to do is insert the user_id and new group_id (let's say 10) records into the table for those who are part of a specific group_id (like 6). I Have been trying different INSERT WITH SELECT statements but keep getting errors. I search around, but couldn't get one to work properly from the examples given.

Could anyone help me out?

2
  • Which RDBMS (Oracle, SQL Server, PostgreSQL, MySql, etc) are you using? Commented May 6, 2015 at 14:57
  • 1
    Please edit your question and include the query you tried and how the result differs from what you want Commented May 6, 2015 at 14:57

2 Answers 2

1

Check This..

insert into yourtable (user_id,group_id) Select user_id , 10 as Newgroup_id /*The new Group id */ From yourtable where group_id = 6 /*OldGroupID*/ 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! Worked like a charm. Just what I needed. :)
1
insert into table (user_id, group_id) values (12,6), (13,6) 

or

insert into table (user_id, group_id) select user_id, 10 from table where group_id = 6 

4 Comments

add update code too, for generic question your need to use generic answer
and what if you don't know this '6'?
@ErasmoOliveira The question asked about insert.
@miraclefoxx If you don't know the values then you can't insert them.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.