简体   繁体   中英

SQL Query design involving multiple tables

I am working on a MYSQL query design that, in my opinion, is pretty hard. I'm not experienced in SQL, so I found it really dificult. The point is:

I've got the 'ordertable' table which stores the order of some codes (AA, BB, CC..). In another table, 'AllTables' I store the name of a table associated to a code (AA -> tableA). Finally, 'tableA' table stores some data of diferent units (unit1, unit2...).

CASE 1.

ordertable : Order of codes is given like:

+----------------+------+
| split_position | code |
+----------------+------+
|              1 | AA   |
|              2 | BB   |
|              3 | CC   |
|              4 | DD   |
+----------------+------+

CASE 2.

ordertable Order of codes is given like:

+-------+------+------+------+------+
| id    | pos1 | pos2 | pos3 | pos4 |
+-------+------+------+------+------+
| unit1 | AA   | BB   | DD   | CC   | 
| unit2 | CC   | BB   | AA   | DD   | 
| unit3 | BB   | DD   | CC   | AA   |
+-------+------+------+------+------+

In Case 2 we can also find special codes like 'var15':

+-------+------+-------+------+-------+
| id    | pos1 | pos2  | pos3 | pos4  |
+-------+------+-------+------+-------+
| unit1 | AA   | var15 | DD   | var37 | 
| unit2 | CC   | BB    | AA   | DD    | 
+-------+------+-------+------+-------+

In case we find something similar to 'var'+ number the associated table is always the same: 'variable', where de 'id' is the number of the code 'var37' -> id = 37.

variable

+-----+------------+------+--------+
| id  | name       | time | active |
+-----+------------+------+--------+
| 15  | Pedro      |    5 |      1 |
| 17  | Maria      |    4 |      1 |
+-----+------------+------+--------+

Info of tables:

AllTables

+------+------------+
| code | name       |
+------+------------+
| AA   | tableA     |
| BB   | tableB     |
| CC   | tableC     |
| DD   | tableD     |
+------+------------+

tableA

+-------+------+------+--------+
| id    | name | time | active |
+-------+------+------+--------+
| unit1 | Mark |   11 |      1 |
| unit2 | Jame |   20 |      0 |
+-------+------+------+--------+

tableB

+-------+------+------+--------+
| id    | name | time | active |
+-------+------+------+--------+
| unit1 | Mari |   44 |      1 |
| unit3 | nam2 |   57 |      1 |
+-------+------+------+--------+

Given an id='unit1', I'm expecting the next:

Result

+----------------+------+-------+-------+--------+
| split_position | code |  name | time  | active |
+----------------+------+-------+-------+--------+
|              1 | AA   | Mark  |  11   |    1   |
|              2 | BB   | Mari  |  44   |    1   |
|              3 | CC   |       |       |    0   |
|              4 | DD   |       |       |    0   |
+----------------+------+-------+-------+--------+

In case that the id (unit1) does not exists in tableC or tableD, 'split_position' and 'code' associated should appear but in the 'active' field should appear a 0.

it's a bit of a steep learning curve, but basically you have to declare a cursor and loop over the each row in the ordertable and select your data then UNION the result together using dynamic SQL.

check this sqlFiddle

to order by final result by split position ASC just add ORDER BY split_position ASC to the sql variable before executing it like this sqlFiddle

to solve your problem you would need something like the following:

select split_position, code, name, time, active
from
(
select 'tableA' as tablename, id, [name], [time], active
from tableA
union all select 'tableB' as tablename, id, [name], [time], active
from tableB
) as tbls
inner join alltables atbls
on tbls.tablename=atbls.name
inner join ordertable ot
on atbls.code=ot.code
where tbls.id='unit1'

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