仅获取Microsoft SQL

时间:2015-07-22 14:24:54

标签: sql sql-server-2008

我有一张表是映射表t1。它看起来如下:

+-------+---------+-------------+
| Reqid | FIELDID | LISTITEMID  | 
+-------+---------+-------------+
|  219  | 76      |    3548     |
|  219  | 86      |    2382     |
|  220  | 76      |    3548     |
|  220  | 86      |    3491     |
|  221  | 77      |    3550     |
|  221  | 87      |    2387     |
+-----------------+-------------+

现在我想要的是选择具有以下两个

的不同reqIds
select * from t1 where 
(FIELDID='76' and LISTITEMID='3548')    
or (FIELDID='77' and LISTITEMID='3550') 
or ((FIELDID='86' and (LISTITEMID='3491' or LISTITEMID='2380')) 
or (FIELDID='87' and (LISTITEMID='3494' or LISTITEMID='2386'))) order by REQUIREMENTID

这为我提供了具有上述任何一项要求的所有行。但我想要的是获得

 select * from t1 where 
    ((FIELDID='76' and LISTITEMID='3548') or (FIELDID='77' and LISTITEMID='3550')) 
    and 
(((FIELDID='86' and (LISTITEMID='3491' or LISTITEMID='2380')) 
    or (FIELDID='87' and (LISTITEMID='3494' or LISTITEMID='2386')))) order by REQUIREMENTID

以上显然不会返回任何行。但它指向我想要的东西。

查询应返回的内容是 220 ,因为这是唯一包含FieldID = 76和ListItemId = 3548且fieldId = 86且listItemId = 3491的行。

这是否可以在查询中轻松完成,还是必须编写两个单独的查询,然后创建2个数组并比较两个数组中出现的哪些ID?

3 个答案:

答案 0 :(得分:2)

你几乎拥有它。您需要做的是选择符合条件的所有Reqid,并获取Reqid的所有行。这可以通过子查询来完成。

使用两个子查询完成:

SELECT * 
FROM t1 
WHERE Reqid in 
(
    SELECT t11.Reqid 
    FROM t1 as t11
    WHERE 
        (t11.FIELDID='76' AND t11.LISTITEMID='3548') 
        OR (t11.FIELDID='77' AND t11.LISTITEMID='3550')
) 
AND Reqid in 
(
    SELECT t11.Reqid 
    FROM t1 as t11
    WHERE  
        (t11.FIELDID='86' AND (t11.LISTITEMID='3491' OR t11.LISTITEMID='2380')) 
        OR (t11.FIELDID='87' AND (t11.LISTITEMID='3494' OR t11.LISTITEMID='2386'))
)
ORDER BY REQUIREMENTID

这可以使用JOIN进一步转换为单个子查询。

SELECT * 
FROM t1 
WHERE Reqid in 
(
    SELECT t11.Reqid 
    FROM t1 as t11
    JOIN t1 as t12 on t11.Reqid = t12.Reqid
    WHERE 
        ((t11.FIELDID='76' AND t11.LISTITEMID='3548') OR (t11.FIELDID='77' AND t11.LISTITEMID='3550'))
        AND
        (
            (t12.FIELDID='86' AND (t12.LISTITEMID='3491' OR t12.LISTITEMID='2380')) 
            OR (t12.FIELDID='87' AND (t12.LISTITEMID='3494' OR t12.LISTITEMID='2386'))
        )
) 
ORDER BY REQUIREMENTID

答案 1 :(得分:1)

你可以做一个EXIST陈述

SELECT DISTINCT
    t.ReqID
FROM
    t1 t
WHERE
    ((t.FIELDID = '76'
      AND t.LISTITEMID = '3548')
     OR (t.FIELDID = '77'
         AND t.LISTITEMID = '3550'))
    AND EXISTS ( SELECT
                    1
                 FROM
                    t1
                 WHERE
                    t1.Reqid = t.Reqid
                    AND ((t1.FIELDID = '86'
                          AND t1.LISTITEMID IN ('3491','2380'))
                         OR (t1.FIELDID = '87'
                             AND t1.LISTITEMID IN ('3494','2386'))))

答案 2 :(得分:0)

我认为错误是AND,当你需要的是OR。

    select * from t1 where 
((FIELDID='76' and LISTITEMID='3548') or (FIELDID='77' and LISTITEMID='3550')) 
OR
(((FIELDID='86' and (LISTITEMID='3491' or LISTITEMID='2380')) 
or (FIELDID='87' and (LISTITEMID='3494' or LISTITEMID='2386')))) order by REQUIREMENTID