Solved

MySQL Trigger INSERT number of new records based on column value

Posted on 2014-03-10
2
1,198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 27

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 35

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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

705 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