将两个基于另一个的查询合并为一个

时间:2021-07-15 01:13:25

I have two queries, one based partly on the other. Is there a way of combining them into a single query?

我有两个查询,一个基于另一个查询。有没有办法将它们组合成一个查询?

SELECT tblIssues.*, tblIssues.NewsletterLookup
FROM tblIssues
WHERE (((tblIssues.NewsletterLookup)=5));

SELECT tblArea.ID, tblArea.AreaName
FROM tblArea LEFT JOIN Query2 ON tblArea.ID = Query2.[AreaLookup]
WHERE (((tblArea.Dormant)=False) AND ((Query2.tblIssues.NewsletterLookup) Is Null));

2 个解决方案

#1


1  

If you want to do this in a single query without Query2, you can use the equivalent SQL from Query2 as a subquery in your second example:

如果要在没有Query2的单个查询中执行此操作,可以在第二个示例中使用Query2中的等效SQL作为子查询:

SELECT a.ID, a.AreaName
FROM
    tblArea AS a
    LEFT JOIN
        (
            SELECT i.*
            FROM tblIssues AS i
            WHERE i.NewsletterLookup=5
        ) AS sub
    ON a.ID = sub.[AreaLookup]
WHERE
        a.Dormant=False
    AND sub.NewsletterLookup Is Null;

#2


0  

You ment to perform a JOIN like

您可以执行JOIN之类的操作

SELECT ti.*, tblArea.ID, tblArea.AreaName
FROM tblArea ta
LEFT JOIN tblIssues ti ON ta.ID = ti.[AreaLookup]
WHERE (ti.NewsletterLookup=5 OR ti.NewsletterLookup Is Null)
AND ta.Dormant=False;

#1


1  

If you want to do this in a single query without Query2, you can use the equivalent SQL from Query2 as a subquery in your second example:

如果要在没有Query2的单个查询中执行此操作,可以在第二个示例中使用Query2中的等效SQL作为子查询:

SELECT a.ID, a.AreaName
FROM
    tblArea AS a
    LEFT JOIN
        (
            SELECT i.*
            FROM tblIssues AS i
            WHERE i.NewsletterLookup=5
        ) AS sub
    ON a.ID = sub.[AreaLookup]
WHERE
        a.Dormant=False
    AND sub.NewsletterLookup Is Null;

#2


0  

You ment to perform a JOIN like

您可以执行JOIN之类的操作

SELECT ti.*, tblArea.ID, tblArea.AreaName
FROM tblArea ta
LEFT JOIN tblIssues ti ON ta.ID = ti.[AreaLookup]
WHERE (ti.NewsletterLookup=5 OR ti.NewsletterLookup Is Null)
AND ta.Dormant=False;