?
Solved

MySQL Trigger INSERT number of new records based on column value

Posted on 2014-03-10
2
Medium Priority
?
1,215 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 2000 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

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. . .
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

762 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