Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

MYSQL query, loop all rows, based on specific field value run loop with another query

I have 2 tables as the follows:

Purchases:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think you should write a stored procedure for that like this:

CREATE PROCEDURE FILLSP()
        BEGIN
        DECLARE counter INT DEFAULT 0;
        DECLARE count INT;
        DECLARE repeat INT;
        DECLARE pid INT;
        DECLARE i INT;
        SELECT COUNT(*) FROM Purchases INTO count;
        
        WHILE counter < count  DO 
          SELECT Stations FROM Purchases LIMIT counter,1 INTO repeat;
          SELECT Purchase_id FROM Purchases LIMIT counter,1 INTO pid;
          SET i = 0;
          WHILE i < repeat DO
          INSERT INTO allItems VALUES (pid, 17);
          SET i = i + 1;
          END WHILE;
          SET counter = counter + 1;
        END WHILE;
    END

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...