简体   繁体   中英

Relating multiple table on MySQL

I'm a beginner in MySQL and first time ask a question in here, so correct me if I'm wrong..

I have 4 tables like following :

Table A 
A_id   | A_description

Table B
B_id   | B_description

Table C
C_id   | C_description

Table D
D_id   | D_description

Now i want to make a new table which relate that 4 tables :

Table E
A_id  | B_id | C_id | D_id | D_description

The data representation should like this :

A    B    C    D   D_Description
================================
A1-> B1-> C1-> D1 ->Description
            -> D2 ->Description
       -> C2-> D1 ->Description
            -> D2 ->Description
  -> B2-> C1-> D1 ->Description
            -> D2 ->Description
       -> C2-> D1 ->Description
            -> D2 ->Description

and so on for A2 ...

I started to make the database like these :

Table A 
A_id   | A_description

Table B
B_id   | A_id | B_description

which the B_id and A_id is a unique constraint. But i don't know how to make the structure of the remaining tables these way.

So, how is the database structure should be? (Or how the first 4 tables structure should be?) Thanks :)

EDIT : The result table should be like this :

Table A
A_id | A_Description
  1    explanation
  2    another explanation

Table B
A_id | B_id | B_Description
  1     1    description
  2     1    my description
  2     2    your description

Where B_id column can contain the same data if the A_id is different (like unique constraint)

I think you just want cross join:

Select a.id, b.id, c.id, d.id, d.description
From a cross join b cross join c cross join d;

Note: this will fill in all the column values, but that is how SQL result sets normally work. Formatting the rows is usually done at the application layer.

You can save the results in a table, which acts to relate them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM