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

  • SEARCH
  • Home
  • 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 8093511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:31:07+00:00 2026-06-05T20:31:07+00:00

I have two tables like so; id_image foo bar 1 3 5 2 8

  • 0

I have two tables like so;

id_image   foo    bar
1          3      5
2          8      1
3          17     88
7          14     23
8          12     9


id_image   bar    foo
1          2      3
1          5      6
2          18     11
2          10     12
3          8      21
3          17     81
7          29     50
7          1      14
8          10     26
8          27     34

There is a gap in the autoincremented id_image in the first table. In the second table, the id_image refers to the id_image in the first table, and there’s two of each ID in there.

Notice: This table is theoretical. I have no idea where the gap is exactly, or whether or not there are even multiple gaps. All I know is that the first value is 1 and the last value is higher than the total row count.

Now, I’d like to fix this gap.

Before you say that the gaps don’t matter and if they do, it’s bad database design, let me tell you; I agree with you.

However, what I’m dealing with is a (hopelessly rear end backwards) third-party open source system to which I need to import a huge amount of existing data that doesn’t have cross-referenceable IDs into multiple tables. The only way I can make sure that the same data gets a matching ID in every table throughout the system is to input it sequentially, and that means I can’t have gaps.

So what I do now need to do is;

  1. Fix the gap in the id_image column in the first table, so that the last value matches with the row count.
  2. Edit the id_image column in the second table so that its value corresponds to the same row is corresponded to before the gap fix.

How would I begin to do this? I understand that this might be outside the capabilities of the MySQL query language, so PHP answers are also acceptable. Thanks! 🙂

  • 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-06-05T20:31:08+00:00Added an answer on June 5, 2026 at 8:31 pm

    The basic idea here is to find all of the gaps first to determine how much you need to decrement each id. Then, you have to iterate through both tables and apply the decrement. (You’ll need to add: host, db, user, pass, and the actual table names)

    try {
        $pdo = new PDO('mysql:host=HOST;dbname=DB', 'user', 'pass');
    
        $pdo->beginTransaction();
    
        // Iterate through all id's in the first table
        $stmt = $pdo->exec('SELECT image_id FROM TableOne ORDER BY image_id ASC');
        $stmt->bindColumn('image_id', $id);
    
        if(!$stmt->fetch(PDO::FETCH_BOUND)) {
            throw Exception('No rows in table');
        }
    
        $lastId = $id;
        $gaps = array();
    
        // Find all the gaps
        while($stmt->fetch(PDO::FETCH_BOUND)) {
            if($id != ($lastId + 1)) {
                $gaps[] = $id;
            }
    
            $lastId = $id;
        }
    
    
        if(!isset($gaps[0])) {
            throw new Exception('No gaps found');
        }
    
        // For each gap, update the range from the last gap to that gap by subtracting
        // the number of gaps there has been from the id
        $lastGap = $gaps[0];
    
        for($i = 1; $i < count($gaps); $i++) {
            $stmt = $pdo->prepare('UPDATE TableOne SET image_id = image_id - :i WHERE image_id BETWEEN :lastGap AND :gap');
            $stmt->execute(array(
                ':i' => $i,
                ':lastGap' => $lastGap,
                ':gap' => $gaps[$i]
            ));
    
            $stmt = $pdo->prepare('UPDATE TableTwo SET image_id = image_id - :i WHERE image_id BETWEEN :lastGap AND :gap');
            $stmt->execute(array(
                ':i' => $i,
                ':lastGap' => $lastGap,
                ':gap' => $gaps[$i]
            ));
    
            $lastGap = $gaps[$i];
        }
    
        // Finally, fix the gap between the last found gap and the end of the table
        $stmt = $pdo->prepare('UPDATE TableOne SET image_id = image_id - :i WHERE image_id BETWEEN :lastGap AND :gap');
        $stmt->execute(array(
            ':i' => $i,
            ':lastGap' => $lastGap,
            ':gap' => $gaps[$i]
        ));
    
        $stmt = $pdo->prepare('UPDATE TableTwo SET image_id = image_id - :i WHERE image_id BETWEEN :lastGap AND :lastId');
        $stmt->execute(array(
            ':i' => $i,
            ':lastGap' => $lastGap,
            ':lastId' => $lastId
        ));
    
        // Verify everything is correct
        $stmt = $pdo->exec('SELECT image_id FROM TableOne ORDER BY image_id ASC');
        $stmt->bindColumn('image_id', $id);
    
        if(!$stmt->fetch(PDO::FETCH_BOUND)) {
            throw new Exception('No rows'); // Should never be thrown
        }
    
        $lastId = $id;
    
        while($stmt->fetch(PDO::FETCH_BOUND)) {
            if($id != ($lastId + 1)) {
                throw new Exception('There was an error between ids ' . $lastId . ' and '. $id);
            }
    
            $lastId = $id;
        }
    
        $stmt = $pdo->exec('SELECT image_id FROM TableTwo ORDER BY image_id ASC');
        $stmt->bindColumn('image_id', $id);
    
        if(!$stmt->fetch(PDO::FETCH_BOUND)) {
            throw new Exception('No rows in table two'); // Shouldn't hit this
        }
    
        $lastId = $id;
        $ids = array($id);
    
        while($stmt->fetch(PDO::FETCH_BOUND)) {
            $ids[] = $id;
    
            if(count($ids) == 2) {
                if($ids[0] !== $ids[1]) {
                    throw new Exception('Table two error on ids ');
                }
    
                if($ids[0] !== $lastId) {
                    throw new Exception('Table two error on id gapfix');
                }
    
                $lastId = $ids[0];
                $ids = array();
            }
        }
    
        $pdo->commit();
    } catch(Exception $e) {
        $pdo->rollBack();
    
        var_dump($e);
    }
    

    Important: You might want to throw this in a file and run via the CLI: php -f gapfix.php and include a query before $pdo->commit() that returns a list of all the ids so you can verify the operation worked as expected. If it didn’t, you can roll it back as if nothing happened. The code now checks for itself if the first table is in the right order. It doesn’t however check the second table yet. All checking has been implemented!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables like, CREATE TABLE [dbo].[entry] ( [id] [int] NULL, [service] [int]
I have two tables Department and Employee . Department table looks like this: ID
I have two tables which looks something like this Table Queue int ID; string
I have two tables like of this structure: content (content_id, content_type, user_id, time, comment_count)
I have two MySQL tables something like this: tool_owners: tool_owners_id | user_id | ...
I have two tables in my database that look like that: Customer: C_ID city
I have two tables: Companies: (id, name) Workers: (id, name, country) I would like
I have two table like this table_CN (_id, name, phone, favorite, title) table_EN (_id,
I have two tables like the ones below. I need to find what exchangeRate
I have two tables, products and product_images. I have a third table 'product_categorys' too

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.