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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:46:05+00:00 2026-06-08T04:46:05+00:00

I have three tables, A B and C . For every entry in A

  • 0

I have three tables, A B and C. For every entry in A x B (where x is a Cartesian product, or cross join) there is an entry in C.

In other words, the table for C might look like this, if there were 2 entries for A and 3 for B:

| A_ID | B_ID | C_Val |
----------------------|
|  1   |  1   |  100  |
|  1   |  2   |  56   |
|  1   |  3   |  19   |
|  2   |  1   |  67   |   
|  2   |  2   |  0    |
|  2   |  3   |  99   |

Thus, for any combination of A and B, there’s a value to be looked up in C. I hope this all makes sense.

In practice, the size of A x B may be relatively small for a database, but far too large to populate by hand for testing data. Thus, I would like to randomlly populate C‘s table for whatever data may already be in A and B.

My knowledge of SQL is fairly basic. What I’ve determined I can do so far is get that cartesian product as an inner query, like so:

(SELECT B.B_ID, C.C_ID
 FROM B CROSS JOIN C)

Then I want to say something like follows:

INSERT INTO A(B_ID, C_ID, A_Val) VALUES
    (SELECT B.B_ID, C.C_ID, FLOOR(RAND() * 100)
     FROM B CROSS JOIN C)

Not surprisingly, this doesn’t work. I don’t think its valid syntax to genereate a column on the fly like that, nor to try to insert a whole table as values.

How can I basically convert this normal programming pseudocode to proper SQL?

foreach(A_ID in A){
    foreach(B_ID in B){
        C.insert(A_ID, B_ID, Rand(100));
    }
}
  • 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-08T04:46:07+00:00Added an answer on June 8, 2026 at 4:46 am

    The syntax problem is because:

    INSERT INTO A(B_ID, C_ID, A_Val) VALUES
      (SELECT B.B_ID, C.C_ID, FLOOR(RAND() * 100)
       FROM B CROSS JOIN C)
    

    Should be:

    INSERT INTO A(B_ID, C_ID, A_Val) 
      SELECT B.B_ID, C.C_ID, FLOOR(RAND() * 100)
       FROM B CROSS JOIN C;
    

    (You don’t use VALUES with INSERT/SELECT.)

    However you will still have the problem that RAND() is not evaluated for every row; it will have the same value for every row. Assuming the combination of B_ID and C_ID is unique, you can use something like this:

    INSERT INTO A(B_ID, C_ID, A_Val) 
      SELECT B.B_ID, C.C_ID, ABS(CHEKSUM(RAND(B.B_ID*C.C_ID))) % 100
       FROM B CROSS JOIN C;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three tables...users, user_info, and quota_levels. They look like this: CREATE TABLE users
I have two tables Plants and Information. For every plant there are many information,
In my html page, I have one table, for every row there are two
I have three tables of data: table: cars [10,000 rows] table: planes [2,000 rows]
I have three tables Author, Book and AuthorBook. AuthorBook table only contains two foreign
I have three tables in oracle db as newitems, itemdetails, ticketitems table. Some dummy
I have 2 tables, defined as such: CREATE TABLE `product` ( `pid` SMALLINT( 5
I have a database table with a column where every entry is a regular
I have three tables. I have to retrieve the data using Linq statement. My
I have three tables (individuals, groups & recordlabels) and want use the information between

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.