Results 1 to 2 of 2

Thread: [newbie] Include latest ID into INSERT?

  1. #1
    Join Date
    Nov 2020
    Posts
    1

    Question [newbie] Include latest ID into INSERT?

    Hello,

    I need to write a script in SQL to import topics + posts into a phpBB forum.

    The issue I have is: How can I reference the last topic ID when adding a post to it, since a post must reference a topic?

    FWIW, it's in MariaDB, but maybe there's a generic SQL trick to do this.

    Code:
    #Create new topic (ie. discussion thread)
    
    INSERT INTO `phpbb_topics` (forum_id,topic_title,topic_poster,topic_time) VALUES ();
    
    #Add first post to topic
    
    #INSERT INTO `phpbb_posts` (topic_id,forum_id,poster_id,post_time,post_username,post_text) VALUES ();
    
    INSERT INTO `phpbb_posts` (SELECT LAST_INSERT_ID(),forum_id,poster_id,post_time,post_username,post_text) VALUES ();
    Can it be done in SQL, or should I write it in eg. Python?

    Thank you.

  2. #2
    Join Date
    Nov 2020
    Posts
    26
    Possibly:

    INSERT INTO 'phpbb_posts' (forum_id,topic_title,topic_poster,topic_time) VALUES ((SELECT LAST_INSERT_ID()), other data...);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •