Inserting Multiple Rows in MySQL Easily? - mysql

I'm having a bit of trouble. I need to award an item to users on our site, but I don't want to manually fill in the numbers one by one. Is there a way to set the SQL query to INSERT INTO from UID 9 to 5430 without having to create multiple lines? Here's my example.
INSERT INTO `item_owned` (`id`, `uid`, `iid`, `kind`, `time_owned`, `notes`) VALUES (NULL, 'x', '3626', '1', '1592596732', 'NotBanned')
I'm trying to have the "x" be a number, but to have MYSQL generate multiple numbers from 9 to 5430 without having to generate multiple numbers/code all at once. So something like:
INSERT INTO `item_owned` (`id`, `uid`, `iid`, `kind`, `time_owned`, `notes`) VALUES (NULL, '9 - 5430', '3626', '1', '1592596732', 'NotBanned')
The 9 - 5430 is where the issue is. I want to award the item to everyone who has their number between the number 9 and 5430.
Help appreciated - thanks.

You can use stored procedure in mysql to do this; inside your stored procedure you can use a loop to insert multiple entries. I've provided an example below.
Procedure can be implemented like the code shown here:
delimiter $$
create procedure fill_rows(in start_index int,in termination_point int)
begin
while start_index <= termination_point do
INSERT INTO `item_owned` (`id`, `uid`, `iid`, `kind`, `time_owned`, `notes`) VALUES (NULL, start_index, '3626', '1', '1592596732', 'NotBanned');
set start_index := start_index + 1;
end while;
end $$
delimiter ;
Now whenever you want to insert uid let's say from range x to y. Assume x = 10 and y = 1000 then you can simply insert this records using this one time procedure call like:
call fill_row(10, 1000);
This call will insert 990 new rows with uid values 10, 11, 12 ...1000.
Hope this may help you!

Related

Create sql procedure but it's not appeared in the table

there is no error but also doesn't show success
at first, it shows on only table registrations and not the registration_details, and now doesn't appear in both table.
set foreign_key_checks = 0;
drop procedure if exists createRegist;
delimiter //
create procedure createRegist()
begin
declare total_credit float;
declare registration_id INT;
declare credit float;
-- create first registration for student 1
set total_credit = 0;
insert into `student_regist`.`registrations` (`registration_id`, `student_id`,`total_credit`)
values (1, 1, total_credit);
SELECT LAST_INSERT_ID() INTO registration_id;
-- create registration detail 1
SELECT
`student_regist`.`courses`.`credit`
INTO credit FROM
`student_regist`.`courses`
WHERE
`student_regist`.`courses`.`course_id` = 1
LIMIT 1;
set total_credit = total_credit + credit;
insert into `student_regist`.`registration_details` (`registration_details_id`, `registration_id`, `course_id`, `semester`)
values (1, 1, 1, 1);
SELECT 'Success';
end//
delimiter ;
You have not provided nearly enough detail for us to provide any concrete answers. Adding the DDL for your tables to your question is the minimum required for a real answer.
That said, here are some suggestions.
We know nothing of the values you need to store in credit (and total_credit) but it seems likely that it should be DECIMAL, and not FLOAT. Searching decimal vs float on here returns Float vs Decimal in ActiveRecord as the first result.
If you are using MySQL Workbench the errors/warnings should be displayed in the Output Area (View -> Panels -> Show Output Area). Or you could run SHOW WARNINGS; after calling your SP.
CALL createRegist();
SHOW WARNINGS;
Your first insert into registrations uses a hardcoded value of 1 for registration_id, which is presumably the primary key (PK) for the table. The second time you execute the SP and it tries to insert 1 into your PK, it will fail with a duplicate key error -
Error Code: 1062. Duplicate entry '1' for key 'registrations.PRIMARY'
You then follow up with the call for LAST_INSERT_ID() which will not work as you are expecting. From the MySQL docs -LAST_INSERT_ID()
The value of LAST_INSERT_ID() is not changed if you set the AUTO_INCREMENT column of a row to a non-“magic” value (that is, a value that is not NULL and not 0).
Changing the value passed in the insert statement to NULL or 0 (or removing completely) will resolve this -
/* Passing in value of NULL */
set total_credit = 0;
insert into `student_regist`.`registrations` (`registration_id`, `student_id`,`total_credit`)
values (NULL, 1, total_credit);
/* or removing completely */
set total_credit = 0;
insert into `student_regist`.`registrations` (`student_id`,`total_credit`)
values (1, total_credit);

inserting multiple(many ) rows into table in one time

how to inset into mysql table,example INSERT INTO users(user_id,time,)VALUES(1-500,1524275145) in my case need insert into TABLE users 500 rows form 1-500(it's users id) with same values time for all users.
I tried this one, it is working:
First you execute this snippet to create a procedure in your database so that we can call it later (the second snippet) and execute the query that will insert the 1-500 values into the users user_id column. (You can see the two variables that have values of 500 and 1, and you can see the line: VALUES (user_id_val, 1524275145) where the query will be executed.)
First snippet: (try to execute it separately from the next one)
DROP PROCEDURE IF EXISTS insert_loop;
DELIMITER #
CREATE PROCEDURE insert_loop()
BEGIN
DECLARE user_id_max INT UNSIGNED DEFAULT 500;
DECLARE user_id_val INT UNSIGNED DEFAULT 1;
START transaction;
WHILE user_id_val < user_id_max+1 DO
INSERT INTO users (user_id, time)
VALUES (user_id_val, 1524275145);
SET user_id_val = user_id_val + 1;
END WHILE;
COMMIT;
END #
Second snippet:
DELIMITER ;
CALL insert_loop();
Execute this after the first one (separately).
An example query for your case would be like this:
INSERT INTO users (user_id,time) VALUES
(1, 1524275145),
(2, 1524275145),
(3, 1524275145),
(4, 1524275145);
And so on, until 500. The best approach would be to create the query in a for loop.

MYSQL BEFORE INSERT trigger does not insert record into table

I am using MYSQL 5.7.19 on ubuntu 16.04 (local mashine). I have 2 tables schema:
CREATE TABLE raw (rdate DATE, pim int(3));
CREATE TABLE indx (idate DATE, ipim int(2));
INSERT INTO raw (rdate, pim)
VALUES (20000101,50), (20000201, 52);
INSERT INTO indx (idate, ipim)
VALUES (20000101,5), (20000201, 5);
Table raw is for monthly data date and pim numbers. I want to write the trigger - before insert which will compare new pim with latest (is new pim higher or lower compared to previous pim value) and then according to number ranges (for example: if pim is between 40 and 50 than ipim = 5) insert particular number into column ipim in table indx and the new rdate into new idate.
Here is my trigger code:
DELIMITER //
CREATE TRIGGER sortpim BEFORE INSERT ON raw
FOR EACH ROW
BEGIN
DECLARE new_pim,old_pim,max_d INT;
SET new_pim = NEW.pim;
SET old_pim = (SELECT pim FROM raw WHERE rdate > CURDATE());
IF (old_pim <= new_pim) THEN
CASE WHEN new_pim BETWEEN 50 AND 59 THEN
INSERT INTO indx(idate,ipim) VALUES(NEW.rdate,6);
WHEN new_pim BETWEEN 60 AND 70 THEN
INSERT INTO indx(idate,ipim) VALUES(NEW.rdate,7);
END CASE;
ELSEIF (old_pim > new_pim) THEN
CASE
WHEN new_pim BETWEEN 50 AND 60 THEN
INSERT INTO indx(idate,ipim) VALUES(NEW.rdate,5);
WHEN new_pim BETWEEN 40 AND 49 THEN
INSERT INTO indx(idate,ipim) VALUES(NEW.rdate,4);
END CASE;
END IF;
END; //
DELIMITER ;
The trigger code is accepted with no errors but when I try to trigger it by inserting new record into raw table:
INSERT INTO raw(rdate,pim) VALUES(20000301,55);
record is inserted with no errors and the trigger does not do anything - the indx table is not inserted with new record.
I'm new in MYSQL thus it is likely I choose wrong logic or methods. And it could be mistakes in algorithm or syntax. Any suggestions to solve this is appreciated. Thanx.
UPDATE!
Thanx for suggestions and point where are my mistake.
I have found the solution for getting the latest date as there was the mistake in trigger code on line 7. Here is what works:
` SELECT pim FROM raw ORDER BY rdate DESC LIMIT 1; `

MySQL Trigger Error using variables

I'm trying to get the following code to run in a MySQL Trigger but I get error 1064 when I try to save it.
SET #ma = (SELECT modem_alias FROM `play`.`veh` WHERE meid = new.org_a LIMIT 1);
INSERT INTO `play`.`des` (`indx`, `des_a`, `des_b`) VALUES (NULL, new.org_a, SELECT #ma);
The trigger is set to run on 'org' table after an INSERT
Don't use SELECT in the INSERT, just the variable:
INSERT INTO `play`.`des` (`indx`, `des_a`, `des_b`) VALUES (NULL, new.org_a, #ma);
You can also combine the two queries so you don't need a variable:
INSERT INTO play.des (indx, des_a, des_b)
SELECT NULL, new.org_a, modem_alias
FROM play.veh
WHERE meid = new.org_a
LIMIT 1
Your values is wrong: You can't select there, just use the variable itself:
INSERT ... VALUES (..., #ma);

SQL stored precedure 2 insertstatements with 1 select needed for one insert

I got a website with a database running on MySQL Community Server (GPL) version 5.5.37
And I want to write a stored precedure to insert an image in table 1 then select the id from that last post to insert that id in table 2.
I started to google and ended up with this:
CREATE PROCEDURE InsertNewMedia(
IN insertLocatie varchar(255),
IN insertNaam varchar(150),
IN insertOmschrijving longtext,
IN insertCategorieID int
)
BEGIN
INSERT INTO MediaDB (idMediaDB, Locatie, Naam, Omschrijving) VALUES (NULL, insertLocatie, insertNaam, insertOmschrijving);
SELECT MAX(idMediaDB) AS Nieuwste FROM MediaDB;
INSERT INTO MediaLink (idMediaLink, OverMijShoots_idOverMijShoots, MediaDB_idMediaDB) VALUES (NULL, insertCategorieID, Nieuwste);
END
I know this is a wrong code but I can't find a good awnser on what I need.
Can anyone help me?
I think you are looking for LAST_INSERT_ID():
delimiter //
CREATE PROCEDURE InsertNewMedia(
IN insertLocatie varchar(255),
IN insertNaam varchar(150),
IN insertOmschrijving longtext,
IN insertCategorieID int
)
BEGIN
INSERT INTO MediaDB(idMediaDB, Locatie, Naam, Omschrijving)
VALUES (NULL, insertLocatie, insertNaam, insertOmschrijving);
INSERT INTO MediaLink (idMediaLink, OverMijShoots_idOverMijShoots, MediaDB_idMediaDB)
VALUES (NULL, insertCategorieID, LAST_INSERT_ID());
END//
delimiter ;