Don
Don

Reputation: 4192

Insert a new record for all unique field values in same table?

I have a table, login.user_tools with columns, id, userid and tool where id is the serialized unique id. there are duplicate records for userid.

I know I can get the distinct userids using SELECT DISTINCT userid FROM login.user_tools.

How can I INSERT a new record for every unique user id in the table with a new tool value?

Upvotes: 0

Views: 48

Answers (1)

Oto Shavadze
Oto Shavadze

Reputation: 42753

(Please, before doing insert, make table copy, anyway)

If I correctly understand, you need simply insert .. select

insert into table_name
(userid , tool)
SELECT userid , 'new_tool_value'  FROM table_name GROUP BY userid 

Upvotes: 1

Related Questions