我有以下3个表格。如果在表2或表3中找到匹配的记录,我需要一个查询来从表1中进行选择。
表1: 的recordId, 组ID, 名称
表2: RecordID(Table1.RecordID), 子ID, AFLAG
表3: GroupID(Table1.GroupID), 子ID, AFLAG
答案 0 :(得分:1)
这是相当直接的,只需使用左连接并匹配最终连接表存在的ID
SELECT t1.* FROM table1 t2
LEFT JOIN table2 t2 ON t1.RecordID = t2.RecordID
LEFT JOIN table3 t3 ON t1.GroupID = t3.GroupID
WHERE t2.SubID IS NOT NULL OR t3.SubID IS NOT NULL
这应该让你更接近你的最终目标
答案 1 :(得分:1)
布莱恩的帖子这么快! 或者......
select *
from table1
where (recordid in (select recordid from table2) )
OR (recordid in (select recordid from table3) )
答案 2 :(得分:0)
我认为你的基本查询看起来有点像这样:
SELECT *
FROM Table1 T1
WHERE EXISTS ( SELECT 1 FROM Table2 T2 WHERE T1.RecordId = T2.RecordId )
OR EXISTS ( SELECT 1 FROM Table3 T3 WHERE T1.GroupId = T3.GroupId )