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 961491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:21:09+00:00 2026-05-16T01:21:09+00:00

I have two tables, each one has a primary ID column as key. I

  • 0

I have two tables, each one has a primary ID column as key. I want the two tables to share one increasing key counter.

For example, when the two tables are empty, and counter = 1. When record A is about to be inserted to table 1, its ID will be 1 and the counter will be increased to 2. When record B is about to be inserted to table 2, its ID will be 2 and the counter will be increased to 3. When record C is about to be inserted to table 1 again, its ID will be 3 and so on.

I am using PHP as the outside language. Now I have two options:

  1. Keep the counter in the database as a single-row-single-column table. But every time I add things to table A or B, I need to update this counter table.

  2. I can keep the counter as a global variable in PHP. But then I need to initialize the counter from the maximum key of the two tables at the start of apache, which I have no idea how to do.

Any suggestion for this?

The background is, I want to display a mix of records from the two tables in either ASC or DESC order of the creation time of the records. Furthermore, the records will be displayed in page-style, say, 50 records per page. Records are only added to the database rather than being removed. Following my above implementation, I can just perform a “select … where key between 1 and 50” from two tables and merge the select datasets together, sort the 50 records according to IDs and display them.

Is there any other idea of implementing this requirement?

Thank you very much

  • 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-16T01:21:10+00:00Added an answer on May 16, 2026 at 1:21 am

    Well, you will gain next to nothing with this setup; if you just keep the datetime of the insert you can easily do

    SELECT * FROM 
    (
    SELECT columnA, columnB, inserttime
    FROM table1
    UNION ALL
    SELECT columnA, columnB, inserttime
    FROM table2
    ) 
    ORDER BY inserttime
    LIMIT 1, 50
    

    And it will perform decently.

    Alternatively (if chasing last drop of preformance), if you are merging the results it can be an indicator to merge the tables (why have two tables anyway if you are merging the results).

    Or do it as SQL subclass (then you can have one table maintain IDs and other common attributes, and the other two reference the common ID sequence as foreign key).

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

Sidebar

Related Questions

I have two tables, one with categories and subcategories. Each category and subcategory has
I have two database tables: 'MyTable' which has a standard auto-incrementing primary key integer
i have two tables, one 'master' and one 'child' table. Each table has a
I have a problem with mutual constraints. I want to have two tables each
I have two tables table1 and table2 each having a column named email along
i have two tables products and reviews each product has several reviews linked by
I have two tables: Folder, Files. Each table has 4 fields: ParentID, ID, Name,
I have two tables - 'business' and 'business_contacts'. The business_contact table has a many-to-one
For example, say I have two tables, Product and Order . Product has ID
The Setup I have two tables, USERS and PETS . each user has an

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.