Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MySQL Trigger INSERT number of new records based on column value

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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
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

650 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