简体   繁体   English

涉及多个表的SQL查询设计

[英]SQL Query design involving multiple tables

I am working on a MYSQL query design that, in my opinion, is pretty hard. 我认为我正在努力设计一个非常困难的MYSQL查询设计。 I'm not experienced in SQL, so I found it really dificult. 我没有SQL方面的经验,因此发现它确实很困难。 The point is: 重点是:

I've got the 'ordertable' table which stores the order of some codes (AA, BB, CC..). 我有一个“ ordertable”表,其中存储了一些代码(AA,BB,CC ..)的顺序。 In another table, 'AllTables' I store the name of a table associated to a code (AA -> tableA). 在另一个表“ AllTables”中,我存储与代码关联的表的名称(AA-> tableA)。 Finally, 'tableA' table stores some data of diferent units (unit1, unit2...). 最后,“ tableA”表存储了不同单位(unit1,unit2 ...)的一些数据。

CASE 1. 情况1。

ordertable : Order of codes is given like: ordertable :代码顺序如下:

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

CASE 2. 案例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': 案例2中,我们还可以找到特殊代码,例如'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. 如果我们发现与“ var” +数字相似的内容,则关联表始终相同:“ variable”,其中,“ id”是代码“ var37”的编号-> id = 37。

variable 变量

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

Info of tables: 表信息:

AllTables AllTables

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

tableA TABLEA

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

tableB tableB的

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

Given an id='unit1', I'm expecting the next: 给定一个id ='unit1',我期望下一个:

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. 如果在tableC或tableD中不存在id(unit1),则应显示“ split_position”和“ code”相关联,但在“ active”字段中应显示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. 这是一条陡峭的学习曲线,但是基本上,您必须声明一个游标并在订单表的每一行上循环,然后选择数据,然后使用动态SQL将结果统一在一起。

check this sqlFiddle 检查这个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 ORDER BY split_position ASC拆分位置ASC的最终结果排序,只需在执行此sqlFiddle之类的操作之前将ORDER BY split_position ASC添加到sql变量

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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