需要SQL语句帮助:查询中的查询

时间:2016-01-13 18:54:52

标签: sql

我正在进行一项查询,我需要编制一个成员列表,其中包含与特定网站相关联的特定产品组,然后为该列表中的每个成员检索其帐户中的所有产品

查询的第一部分是:

SELECT        
    MEMBERS.memid
FROM            
    SITES 
INNER JOIN
    MEMBERS ON SITES.siteid = MEMBERS.siteid 
INNER JOIN
    SS ON MEMBERS.memid = SS.memid 
INNER JOIN
    PRODUCTCATS 
INNER JOIN
    PRODUCTS ON PRODUCTCATS.productcatid = PRODUCTS.productcatid 
    ON SS.productid = PRODUCTS.productid 
INNER JOIN
    EMPLOYEES ON SS.employeeid = EMPLOYEES.employeeid
WHERE        
    (PRODUCTS.productcatid = 77) 
    AND (SS.initialdate BETWEEN @rvPurchaseStart AND @rvPurchaseEnd)

从该列表中,我需要为该列表中的每个成员运行以下查询:

SELECT        
    SITES.sitename, MEMBERS.scancode, MEMBERS.lname, 
    MEMBERS.fname, MEMBERS.mtypeid, MEMBERS.status, 
    PRODUCTS.description, SS.initialdate, 
    SS.initialquantity, SS.usedquantity, SS.dateexpire, 
    EMPLOYEES.lname AS Expr1, EMPLOYEES.fname AS Expr2
FROM             
    SITES 
INNER JOIN
    MEMBERS ON SITES.siteid = MEMBERS.siteid 
INNER JOIN
    SS ON MEMBERS.memid = SS.memid 
INNER JOIN
    PRODUCTCATS 
INNER JOIN
    PRODUCTS ON PRODUCTCATS.productcatid = PRODUCTS.productcatid 
    ON SS.productid = PRODUCTS.productid 
INNER JOIN
    EMPLOYEES ON SS.employeeid = EMPLOYEES.employeeid
WHERE        
    (PRODUCTS.productcatid <> '68') 
    AND (PRODUCTS.departmentid = '5') 
    AND (MEMBERS.status = 'A') 
    AND (SS.usedquantity < SS.initialquantity) 
    AND (PRODUCTS.scancode <> 'PASSCOMP1' OR
         PRODUCTS.scancode <> 'PASSCOMP3' OR
         PRODUCTS.scancode <> 'PASSCOMP5') 
    AND (PRODUCTS.inactive = 'False')

我非常感谢帮助!!!

1 个答案:

答案 0 :(得分:0)

像这样的Sometnihg?只需将第一个查询作为表添加到第二个查询中。

SELECT 
    SITES.sitename, MEMBERS.scancode, 
    MEMBERS.lname, MEMBERS.fname, MEMBERS.mtypeid, 
    MEMBERS.status, PRODUCTS.description, SS.initialdate, 
    SS.initialquantity, SS.usedquantity, SS.dateexpire, 
    EMPLOYEES.lname AS Expr1, EMPLOYEES.fname AS Expr2 
FROM 
    SITES 
INNER JOIN 
    MEMBERS ON SITES.siteid = MEMBERS.siteid 
INNER JOIN 
    SS ON MEMBERS.memid = SS.memid 
INNER JOIN 
    PRODUCTCATS 
INNER JOIN 
    PRODUCTS ON PRODUCTCATS.productcatid = PRODUCTS.productcatid 
       ON SS.productid = PRODUCTS.productid 
INNER JOIN 
    EMPLOYEES ON SS.employeeid = EMPLOYEES.employeeid 
INNER JOIN 
    (SELECT 
         MEMBERS.memid 
     FROM 
         SITES 
     INNER JOIN 
         MEMBERS ON SITES.siteid = MEMBERS.siteid 
     INNER JOIN 
         SS ON MEMBERS.memid = SS.memid 
     INNER JOIN 
         PRODUCTCATS 
     INNER JOIN 
         PRODUCTS ON PRODUCTCATS.productcatid = PRODUCTS.productcatid 
             ON SS.productid = PRODUCTS.productid 
     INNER JOIN 
         EMPLOYEES ON SS.employeeid = EMPLOYEES.employeeid 
     WHERE 
         (PRODUCTS.productcatid = 77) 
         AND (SS.initialdate BETWEEN @rvPurchaseStart AND @rvPurchaseEnd)) TMP_TABLE ON MEMBERS.memid = TMP_TABLE.memid 
WHERE 
    (PRODUCTS.productcatid <> '68') 
    AND (PRODUCTS.departmentid = '5') 
    AND (MEMBERS.status = 'A') 
    AND (SS.usedquantity < SS.initialquantity) 
    AND (PRODUCTS.scancode <> 'PASSCOMP1' OR PRODUCTS.scancode <> 'PASSCOMP3' OR PRODUCTS.scancode <> 'PASSCOMP5') 
    AND (PRODUCTS.inactive = 'False')