TSN2003
TSN2003

Reputation: 19

insert into values where

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?

Upvotes: 1

Views: 575

Answers (2)

paparazzo
paparazzo

Reputation: 45106

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 

Upvotes: 1

Abdul Rehman Sayed
Abdul Rehman Sayed

Reputation: 6672

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*/

Upvotes: 1

Related Questions