SQL查询设计涉及多个表

时间:2014-03-19 00:01:40

标签: mysql sql join

我正在研究一种MYSQL查询设计,在我看来,这很难。我没有SQL经验,所以我发现它真的很难。重点是:

我有'ordertable'表,它存储了一些代码(AA,BB,CC ..)的顺序。在另一个表中,'AllTables'我存储了与代码相关联的表的名称(AA - > tableA)。最后,'tableA'表存储了一些不同单位的数据(unit1,unit2 ......)。

案例1。

ordertable :代码顺序如下:

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

案例2。

ordertable 代码顺序如下:

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

案例2 中,我们还可以找到特殊代码,例如'var15':

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

如果我们发现类似于'var'+ number的内容,则相关联的表始终相同:'variable',其中de'id'是代码'var37'的编号 - > id = 37。

可变

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

表格信息:

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 |
+-------+------+------+--------+

鉴于id ='unit1',我期待下一个:

结果

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

如果tableC或tableD中不存在id(unit1),则应显示“split_position”和“code”关联,但在“active”字段中应显示为0.

2 个答案:

答案 0 :(得分:1)

这是一个陡峭的学习曲线,但基本上你必须声明一个游标和循环 在ordertable中的每一行上选择你的数据,然后使用动态SQL将结果联合起来。

检查此sqlFiddle

按分割位置ASC的最终结果排序只需将ORDER BY split_position ASC添加到sql变量中,然后执行它sqlFiddle

答案 1 :(得分:0)

要解决您的问题,您需要以下内容:

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'