Chris G.
Chris G.

Reputation: 3981

Inserting new records programatically in MySQL

I need to accomplish a purely administrative scripting task in mysql and I'd like to do it without writing a perl or php script or something.

Given the following Table A:

user_id | permission
--------------------
1         1
4         1
298       1

How would I give every user_id present in Table A the permission 5. (It needs to insert a new row for each user_id present in the table)

Upvotes: 0

Views: 51

Answers (2)

Luke Z
Luke Z

Reputation: 2429

insert into tableA (user_id, permission) 
select distinct user_id, 5 from tableA

To give every unique user a new permission entry.

Upvotes: 0

fancyPants
fancyPants

Reputation: 51928

To insert for each user in the table another row with permission 5 you can do

INSERT INTO TableA (user_id, permission)
SELECT DISTINCT user_id, 5
FROM TableA;

Upvotes: 2

Related Questions