0

首先让我说是的,这是一种存储数据的可怕方式,其次,这不是我的错 :) 我正在尝试与第 3 方数据库集成以提取存储在 3 个表中的信息,这确实应该已经在两个 AND 中存储,其中表 2 具有多对一关系。既然不是这样,我有一个难题要分享。

表一包含可以存储多个值的行。每行都有codeid1- codeid20。这些列可能包含一个值或 0(它们从不null)。它们也有一个对应的codetype1-codetype20将是 0 或 1。

如果codetype1等于 0,我们转到表 2 并description从匹配项中进行选择table1.codeid1=table2.id。如果codetype1等于 1,我们现在必须查看 table3 并找到位置table1.codeid1=table3.id,然后匹配table3.table2id=table2.id并返回描述。

下面是数据结构:

table1
codeid1,codeid2,codeid3,...codeid20 ... codetype1,codetype2,codetype3,.....codetype20
18      13      1          33           0         0         1              1
13      21      45         0            0         1         0              0


table2
id,    description
13    Item 13 Description
15    Item 15 Description
17    Item 17 Description
18    Item 18 Description
21    Item 21 Description
28    Item 28 Description
45    Item 45 Description

table3
id,  table2id
1    15
33   17
21   28

我要寻找的结果如下所示:

rowid, description
1      Item 18 Description
1      Item 13 Description
1      Item 15 Description
1      Item 17 Description
2      Item 13 Description
2      Item 28 Description
2      Item 45 Description

昨晚我开始与某人合作,但我错过了未集成 table3 的情况的部分复杂性。就像我说的,有趣的谜题......这给了我前两张桌子之间的关系,但我不确定如何在第三张桌子上工作。

SELECT table1.rowid, table2.description
FROM table2
INNER JOIN table1 
ON table2.id=table1.codeie1
OR table2.id=table1.codeie2
...

数据库是通过ODBC连接的Faircom C-Tree DB,一般兼容Mysql语句,包括UNION、WITH、INTERSECT、EXISTS、JOIN……没有PIVOT功能。

https://docs.faircom.com/doc/sqlref/sqlref.pdf

4

1 回答 1

0

也许它会与or而不是一起工作in

where exists (select 1
              from table1 as t1
              where t2.id = t1.codeid1 or
                    t2.id = t2.codeid2 or
                    . . .
             );
于 2018-01-26T16:01:40.990 回答