I am trying to insert data into my database but I want the data to be assigned to all numbers indicated by profile_id. My table is called “profile_meta”
So the table structure is:
profile_id | meta_key | meta_value
I want to have an SQL command that will insert the word “userclass” in the meta_key area, the word “Command” in the meta_value and the profile_id to have numbers ranging from 1 to 554.
Okay, using the SQL command, I want to add data to a table within my database. The table is “profile_meta”. The structure of the table is: profile_id | meta_key | meta_value and so basically what I want to do is database for each column without having to do it 554 times for each profile_id number
Any ideas how to do this?
Advertisement
Answer
It looks like you’re trying to insert a bunch of values, you can use a loop, inside a stored procedure, and then delete the stored procedure, if you want:
DELIMITER $$ DROP PROCEDURE IF EXISTS InsertMetaValues$$ CREATE PROCEDURE InsertMetaValues() BEGIN DECLARE x INT; SET x = 1; WHILE x <= 554 DO INSERT INTO profile_meta(profile_id,meta_key,meta_value) VALUES(x,'userclass','Command'); SET x = x + 1; END WHILE; END$$ DELIMITER ; CALL InsertMetaValues(); # IF you want to delete it after it finishes: # DROP PROCEDURE IF EXISTS InsertMetaValues;