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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:40:10+00:00 2026-06-16T23:40:10+00:00

I have a situation where multiple tables contain similar information based on country. Changing

  • 0

I have a situation where multiple tables contain similar information based on country. Changing the database schema is not an option, I’m not interested in the use of a stored procedure. I’d prefer to grab everything in a single query returning multiple rows than performing one query for every country.

First, I have a definitions table which lists all the countries we have tables for:

countries
+============+========+
| country_id | prefix |
+============+========+
| 1          | us     |
+------------+--------+
| 2          | ca     |
+------------+--------+

Secondly, I have a relationships table:

relationships
+========+============+==============+
| rel_id | country_id | upc          |
+========+============+==============+
| 1      | 1          | 111111111111 |
+--------+------------+--------------+
| 2      | 2          | 111111111111 |
+--------+------------+--------------+
| 3      | 1          | 222222222222 |
+--------+------------+--------------+
| 4      | 2          | 222222222222 |
+--------+------------+--------------+
| 5      | 2          | 333333333333 |
+--------+------------+--------------+
| 6      | 1          | 444444444444 |
+--------+------------+--------------+

Then, I have two tables named “us_products” and “ca_products”. If an entry exists in the countries table, then a table exists with the name [countries.prefix]_products. All *_products tables are identical to one another. Same columns and same data types.

us_products
+============+==============+=======+
| product_id | upc          | title |
+============+==============+=======+
| 1          | 111111111111 | Shoe! |
+------------+--------------+=======+
| 2          | 222222222222 | Tie   |
+------------+--------------+=======+
| 3          | 444444444444 | Sock  |
+------------+--------------+=======+

ca_products
+============+==============+=======+
| product_id | upc          | title |
+============+==============+=======+
| 1          | 111111111111 | Shoe. |
+------------+--------------+=======+
| 2          | 222222222222 | Tie   |
+------------+--------------+=======+
| 3          | 333333333333 | Shirt |
+------------+--------------+=======+

The goal is to have a query formatted something similar to the following (obviously this doesn’t work, otherwise I wouldn’t be asking this question…):

SELECT
  countries.prefix,
  products.title
FROM
  relationships
INNER JOIN
  [countries.prefix]_products AS products
  ON
  relationships.upc = products.upc
WHERE
  relationships.upc = '111111111111'

Should return:

+========+=======+
| prefix | title |
+========+=======+
| us     | Shoe! |
+--------+-------+
| ca     | Shoe. |
+--------+-------+

Thanks for the help! If the ONLY way of doing this is through a stored procedure then I supposed I don’t have any other option, in which case would you mind throwing together an example procedure and query which would perform on the above mentioned table structures?

  • 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-16T23:40:11+00:00Added an answer on June 16, 2026 at 11:40 pm

    If you don’t want to create a view, you can use a common table expression as a “temporary” view:

    with normalized_products as (
       select 1 as country_id, 
              product_id, 
              upc,
              title 
       from us_products
       union all
       select 2 as country_id, 
              product_id, 
              upc,
              title 
       from ca_products
    ) 
    SELECT countries.prefix,
           products.title
    FROM relationships as rel
      JOIN normalized_products as prod
        ON rel.upc = prod.upc
       and rel.country_id = prod.country_id
    where rel.upc = '111111111111'
    

    But again: do fix your datamodel. This is going to hurt you more and more

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

Sidebar

Related Questions

I have a database schema that contains multiple tables with different names, but all
We have multiple tables that contain static key/value pairs, which are currently pulled using
I have a situation where I am able to choose from multiple tables in
Current situation I have two tables in my database, one for posts, and one
I have the following situation: An Conversations entity/table which has multiple Tags associated to
I have the following situation: multiple virtual directories under same application pool in IIS
The situation: I have multiple classes that should each hold a variable with a
I have a situation where I return results with multiple rows. I'm looking for
I have a situation where a Java Applet hangs after being opened multiple times.
I have a simple parent-child situation where the parent can have multiple children. The

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.