Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 568927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:14:26+00:00 2026-05-13T13:14:26+00:00

I have a simple insert select which insert _TABLE_B_ data in _TABLE_A_ new row

  • 0

I have a simple insert select which insert _TABLE_B_ data in _TABLE_A_ new row

INSERT INTO _TABLE_A_(_USERNAME_,_ID_) 
SELECT  _USERNAME_,_ID_ 
FROM _TABLE_B_ 

I want to insert a row in a table named _TABLE_C_ each time i insert a row in _TABLE_A_ and add the current inserted _TABLE_C_ id in _TABLE_A_.

i’ll try to explain it in an other way :

INSERT INTO _TABLE_A_(_USERNAME_,_ID_,_FOREIGN_ID_) 
SELECT  B._USERNAME_,B._ID_,C._FOREIGN_ID_
FROM _TABLE_B_ AS B 
LEFT JOIN _TABLE_C_ AS C
#Insert a row in _TABLE_C_ to retrieve _FOREIGN_ID_...

I’m searching for a single minimal query which have the INSERT SELECT statement like mine because insert select can loop and i have to loop.

FYI :
I’m in a stored procedure.
I also use prepared statements with dynamic data, and cursors is not suitable for dynamic data select…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:14:26+00:00Added an answer on May 13, 2026 at 1:14 pm

    I found a solution.

    1. create a temporary table and add dynamic select statement which retrieve the primary keys (id)
    2. declare a cursor and select this temporary table id ( variables doesn’t work but temporary tables do )
    3. execute statement to create temporary table
    4. open the cursor and iterate the inserts

    EXAMPLE


      BEGIN
    
    
        DECLARE isDone INT DEFAULT 0;
        DECLARE fetchedmemberWhoWillReceiveMailId int;
    
        DECLARE cur1 CURSOR FOR SELECT id FROM memberWhoWillReceiveMail;
        DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET isDone = 1;
        SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE;
    
        SET @sexe     = VAR_sexe; 
        SET @event    = VAR_eventId;
        SET @subject = VAR_subject;
        SET @body     = VAR_body; 
        SET @to        = VAR_to; 
        SET @from  = VAR_from;
        SET @region    = VAR_region;
        SET @departement      = VAR_departement; 
        SET @age        = VAR_age;
    
        SET @baseSqlStatement =' CREATE TEMPORARY TABLE memberWhoWillReceiveMail SELECT e.id FROM TABLE_A as e LEFT JOIN TABLE_B AS a on a.member_id = e.id';
    
        SET @whereSqlStatement= 'WHERE e.is_visible = 1 AND e.member_group_id IN (10,11) ';
    
        IF (@region!='') THEN  
        SET @whereSqlStatement=  CONCAT(@whereSqlStatement,' AND region=',@region);
        END IF;
    
        IF (@event !=null ) THEN 
        SET @whereSqlStatement=  CONCAT(@whereSqlStatement,' AND m.event_id !=' ,@eventId); 
        END IF;
    
        IF (@sexe!=null ) THEN
        SET @whereSqlStatement=  CONCAT(@whereSqlStatement,' AND e.sexe=',@sexe); 
        END IF;
    
        SET @baseSqlStatement = CONCAT(@baseSqlStatement,@whereSqlStatement);
    
        START TRANSACTION;
        PREPARE stmt1 FROM @baseSqlStatement;
        EXECUTE stmt1; 
        DEALLOCATE PREPARE stmt1;
    
        OPEN cur1; 
        FETCH cur1 INTO fetchedmemberWhoWillReceiveMailId; 
        WHILE NOT isDone DO
            INSERT INTO conversation(created_at,updated_at)VALUES(now(),now());
            INSERT INTO message(created_at,updated_at,from, to, uniqueID)   VALUES(now(),now(),@from,fetchedmemberWhoWillReceiveMailId,LAST_INSERT_ID() );
            FETCH cur1 INTO fetchedmemberWhoWillReceiveMailId; END WHILE; CLOSE cur1;
    
        COMMIT;
        DROP TEMPORARY TABLE IF EXISTS memberWhoWillReceiveMail;
    
        END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 307k
  • Answers 307k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer August 7, 2007 was a Tuesday. May 13, 2026 at 9:28 pm
  • Editorial Team
    Editorial Team added an answer One of the MDS methods is distance scaling and it… May 13, 2026 at 9:28 pm
  • Editorial Team
    Editorial Team added an answer You get exactly what you wrote. Your copying makes a… May 13, 2026 at 9:28 pm

Related Questions

I have a table with a foreign key and a boolean value (and a
Simpler Example Let's try a simpler example, so people can wrap their heads around
I'm having a general discussion with one of my developers about philosophy of structuring
I need to update my tableA based on the insertions in tableB. i have
I have a rather simple command which I occasionally run: BEGIN TRAN T1; truncate

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.