SQL子查询问题

时间:2012-05-08 14:14:46

标签: mysql

我试图以这种方式组合这两个查询,以确定拥有设备的PI是谁(> $ 100K值)。我有能力找到一个PI拥有的大于100k的所有设备。我也有能力查看所有PI。我只是无法将这两个查询结合起来。我尝试过WHERE子查询和EXIST子查询。我希望能够在查询#2中找到PI所在的所有设备(与其PI所有者匹配)。

查询#1以查找特定PI的设备

select Account_No,Inventory_No,Building_No,Room_No,CDDEPT,Location,Normalized_MFG,Manufacturer_Name,Normalized_Model,Name,Serial_Code,CONCAT( '$', FORMAT( Cost, 2 ) ) as Cost, Equipment_Inventory_Normalized.Active 
from Temp_Equipment_Inventory.Equipment_Inventory_Normalized, `paul`.`ROOM`, `paul`.`BLDG`, `paul`.`LABORATORY`, `paul`.`PERSON` 
where (`PERSON`.`ID` = `LABORATORY`.`PI_ID` OR `PERSON`.`ID` = `LABORATORY`.`SUPV_ID`) 
AND `LABORATORY`.`RM_ID` = `ROOM`.`ID` 
AND `LABORATORY`.`ACTIVE` = '1' 
AND `ROOM`.`BLDG_ID` = `BLDG`.`ID` 
AND ((
`BLDG`.`BLDGNUM` = Equipment_Inventory_Normalized.Building_No 
AND Equipment_Inventory_Normalized.Actual_Building IS NULL 
AND (`BLDG`.`BLDGNUM` != '1023' AND `LABORATORY`.`OTHER_LEVEL` != '1' AND `ROOM`.`RMNUM` != '0199')
)OR (
`BLDG`.`BLDGNUM` = Equipment_Inventory_Normalized.Actual_Building AND 
(`BLDG`.`BLDGNUM` != '1023' AND `LABORATORY`.`OTHER_LEVEL` != '1' AND `ROOM`.`RMNUM` != '0199')
)) 
AND ((
`ROOM`.`RMNUM` = Equipment_Inventory_Normalized.Room_No 
AND Equipment_Inventory_Normalized.Actual_Room IS NULL
)OR (
`ROOM`.`RMNUM` = Equipment_Inventory_Normalized.Actual_Room
)) 
AND Equipment_Inventory_Normalized.Active !=0 
AND SurplusPending != '1'
AND Cost >= 100000 
AND `PERSON`.`CANNUM`='810010787'

查找所有PI的查询2

select distinct i.CAN
from CGWarehouse.CCGV10WC w
inner join CGWarehouse.CCGV10IC i
on w.PROJECT_NUMBER=i.SPONSORED_PROJECT
and w.SEQUENCE_NUMBER=i.PROJECT_SEQUENCE
where w.STATUS='A'
and i.PRIN_INVEST_CODE='Y'
and i.DEL_CODE!='Y'
and i.CAN IS NOT NULL

1 个答案:

答案 0 :(得分:2)

也许你在WHERE子句中寻找IN关键字?

请原谅我,但我必须清理一下你的查询格式......这有点像我的强迫症:

SELECT
    Account_No,
    Inventory_No,
    Building_No,
    Room_No,
    CDDEPT,
    Location,
    Normalized_MFG,
    Manufacturer_Name,
    Normalized_Model,
    Name,
    Serial_Code,
    CONCAT('$', FORMAT( Cost, 2 )) AS Cost,
    Equipment_Inventory_Normalized.Active
FROM
    Temp_Equipment_Inventory.Equipment_Inventory_Normalized a,
    `paul`.`ROOM`,
    `paul`.`BLDG`,
    `paul`.`LABORATORY`,
    `paul`.`PERSON` 
WHERE
    (`PERSON`.`ID` = `LABORATORY`.`PI_ID` OR `PERSON`.`ID` = `LABORATORY`.`SUPV_ID`) 
    AND `LABORATORY`.`RM_ID` = `ROOM`.`ID` 
    AND `LABORATORY`.`ACTIVE` = '1' 
    AND `ROOM`.`BLDG_ID` = `BLDG`.`ID` 
    AND (
        (
            `BLDG`.`BLDGNUM` = Equipment_Inventory_Normalized.Building_No 
            AND Equipment_Inventory_Normalized.Actual_Building IS NULL 
            AND `BLDG`.`BLDGNUM` != '1023'
            AND `LABORATORY`.`OTHER_LEVEL` != '1'
            AND `ROOM`.`RMNUM` != '0199'
        ) OR (
            `BLDG`.`BLDGNUM` = Equipment_Inventory_Normalized.Actual_Building
            AND `BLDG`.`BLDGNUM` != '1023'
            AND `LABORATORY`.`OTHER_LEVEL` != '1'
            AND `ROOM`.`RMNUM` != '0199'
        )
    ) 
    AND (
        (
            `ROOM`.`RMNUM` = Equipment_Inventory_Normalized.Room_No 
            AND Equipment_Inventory_Normalized.Actual_Room IS NULL
        ) OR (
            `ROOM`.`RMNUM` = Equipment_Inventory_Normalized.Actual_Room
        )
    ) 
    AND Equipment_Inventory_Normalized.Active !=0 
    AND SurplusPending != '1'
    AND Cost >= 100000 
    AND `PERSON`.`CANNUM` IN ( /* this assumes that the `PERSON`.`CANNUM` column matches up with the CGWarehouse.CCGV10IC.CAN column */
        SELECT DISTINCT i.CAN
        FROM
            CGWarehouse.CCGV10WC w
            INNER JOIN CGWarehouse.CCGV10IC i ON w.PROJECT_NUMBER=i.SPONSORED_PROJECT AND w.SEQUENCE_NUMBER=i.PROJECT_SEQUENCE
        WHERE
            w.STATUS='A'
            AND i.PRIN_INVEST_CODE='Y'
            AND i.DEL_CODE!='Y'
            AND i.CAN IS NOT NULL
    )