Solved

MySQL Trigger INSERT number of new records based on column value

Posted on 2014-03-10
2
1,168 Views
Last Modified: 2014-05-14
This question is a revised version of another currently open question: OPEN QUESTION.

I've distilled the essence of my question into a much simpler form:

I need to write a MySQL trigger event that will INSERT a number of records into `table2` based on the value of a field (quantity) of `table1`.

Thus, if during the initial INSERT (into table1) the column `table1.quantity` = 7; My trigger should INSERT 7 new records into table2.


\\
\\
\\
\\

This is ridiculously simple to execute in PHP... I'm simply running into a wall setting up the counter syntax for MySQL. Many thanks!

Roman A/K/A Rapturer
0
Comment
Question by:Rapturer
2 Comments
 
LVL 26

Expert Comment

by:Zberteoc
ID: 39920475
Here is a tutotial for MySQL trigger with PHP code examples:

http://code.tutsplus.com/articles/introduction-to-mysql-triggers--net-12226
0
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 39920496
Not tested but it's probably something like this:

DELIMITER $$
CREATE TRIGGER foo BEFORE INSTERT ON table1
   FOR EACH ROW BEGIN
     SET @x = 0;
     insertLoop: LOOP
       SET @x = @x + 1;
       
       INSERT INTO table2...
       
       IF @x < NEW.quantity THEN
         ITERATE insertLoop;
       END IF;
       LEAVE insertLoop;
     END LOOP insertLoop;
   END;
$$

Open in new window

0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question