Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为MS Access编写SQL查询_Sql_Ms Access_Vba - Fatal编程技术网

为MS Access编写SQL查询

为MS Access编写SQL查询,sql,ms-access,vba,Sql,Ms Access,Vba,我有两张桌子 表1tblin结构 inst_id instruction 1 aaaaa 2 bbbbb 3 cccccc 4 ddddd 5 eeeee 6 fffff 7 ggggg 8 hhhhh 9 iiiii 10

我有两张桌子

表1tblin结构

inst_id     instruction
1               aaaaa
2               bbbbb
3               cccccc
4               ddddd
5               eeeee
6               fffff
7               ggggg
8               hhhhh
9               iiiii
10              jjjjj
表2tblDischarge

d_id  inst_id1  inst_id2   inst_id3  inst_id4 
1       3          5           2        8      
2       5          7           1        10          
3       10         2           8        7      
4       6          8           4        3      
5       9          1           3        5           
我需要根据指令id1、指令id2、指令id3、指令id4获取d_id=2的所有指令

预期产出

eeeee
ggggg
aaaaa
jjjjj
我正在尝试编写一个SQL查询以获得上述输出。我正在使用MS Access,我需要在MS Access VBA程序中使用此SQL查询

我尝试了以下两个SQL查询,使用In(),使用Exists(),得到了所有指令,而不是特定指令

select instruction from tblInstruction where inst_id 
  in (select inst_id1,inst_id2,inst_id3,inst_id4 from tblDischarge where d_id = 2);

select instruction from tblInstruction where 
  exists(select inst_id1,inst_id2,inst_id3,inst_id4 from tblDischarge Where d_id = 2);

使用以下代码实现结果:-

select instruction from 
table1 inst1
JOIN 
(select inst_id1 as inst_id from table 2 where d_id = 2
UNION 
select inst_id2 as inst_id from table 2 where d_id = 2
UNION
select inst_id3 as inst_id from table 2 where d_id = 2
UNION
select inst_id4 as inst_id from table 2 where d_id = 2
) inst2
on inst1.inst_id  = inst2.inst_id

也许使用
的最有效的方法是:

select i.*
from tblInstruction as i
where exists (select 1 from tblDischarge as d where d.d_id = 2 and i.inst_id = d.inst_id1
             ) or
      exists (select 1 from tblDischarge as d where d.d_id = 2 and i.inst_id = d.inst_id2
             ) or
      exists (select 1 from tblDischarge as d where d.d_id = 2 and i.inst_id = d.inst_id3
             ) or
      exists (select 1 from tblDischarge as d where d.d_id = 2 and i.inst_id = d.inst_id4
             );
请注意,您的数据模型非常糟糕。最好为每个指令/放电对提供一个具有单独行的连接表

编辑:

更好的结构是有一个连接表
InstitutionDischarge
,列如下:

  • institutionDischargeId
  • d_id
  • inst\u id
  • 计数器
    ——或者类似的东西

您的意思是,我需要再创建一个表,您能告诉我这三个表的结构吗。这样我才能纠正错误model@RaviKannan:请不要要求免费劳工。考虑回答一组提示,让你向前移动,在顶部建立更多的工作。如果你养成了询问答案的习惯,并且可以逐字逐句地输入答案,那么你就不会挑战自己作为一名软件工程师(并且可能会要求志愿者提供太多的答案)。请尝试构建一个更好的标题,一个针对你的问题的标题。“MS Access”-我们知道,这是“SQL”标签之一,已经更好地放置在标记中——“查询”——几乎所有SQL问题都将是“需要帮助”——我们知道,这就是为什么您要问一个关于堆栈溢出的问题。你的问题的标题是你有机会“出售”给可能的回答者。给他们一些他们还不知道的信息。