如何返回仅针对特定属性的SQL结果?

时间:2012-05-22 19:50:01

标签: sql

我有一张类似于以下内容的表格:

REL_Aid_To_AttributeValue:
ID    AttributeValueID
101   1319
101   1320
101   1344
101   1345
102   1319
102   1320
102   1321
102   1336
103   1320
103   1336

我有另一个表按类型对这些AttributeValueID进行分组:

TBL_AttributeValues:
AttributeValueID    AttributeTypeID
1319                1
1320                1
1321                1
1336                2
1344                3 
1345                3

我有第三个表格,提供有关第一个表格中每个ID的信息:

TBL_Aids:
ID     Title                Author       etc.
101    Aid About Spiders    John Doe
102    Aid About Mites      Jane Doe 
103    Aid About Beetles    Joe Schmo

我想在AttributeTypeID = 1中返回仅包含AttributeValueID 1319 OR(包含)1320的ID(以及有关它们的信息,如标题,作者等)的结果。所以我希望返回101,因为它只有1319和1320用于AttributeType 1,还有103因为它只有1320用于AttributeType 1.我不想返回102因为虽然它有1319和1320,但它也有1321,它也是AttributeType 1。

我尝试了以下内容:

SELECT a.ID, a.Title, c.AttributeTypeID, 
COUNT(b.AttributeValueID)AS Total
FROM TBL_Aids as a,
REL_Aid_To_AttributeValue as b,
TBL_AttributeValues as c
WHERE a.ID=b.AidID
AND b.AttributeValueID=c.ID
AND a.Status=2
AND AttributeTypeID=1
AND (b.AttributeValueID=1319
OR b.AttributeValueID=1320)
GROUP BY a.ID, a.Title,
c.AttributeTypeID
HAVING COUNT(b.AttributeValueID) <= 2 

但结果仍然返回102,因为它已经被1319和1320限制并且正在计算该总数而不是类型1的AttributeValueID的总数。我是否需要某种子查询来完成此操作?任何帮助,将不胜感激!非常感谢你。

7 个答案:

答案 0 :(得分:2)

尝试:

SELECT DISTINCT A.ID
FROM REL_Aid_To_AttributeValue A
WHERE A.AttributeValueID IN
(
    SELECT AttributeValueID
    FROM TBL_AttributeValues
    WHERE AttributeValueID IN (1319, 1320) AND AttributeTypeID=1
)
AND A.ID NOT IN
(
    SELECT A.ID
    FROM REL_Aid_To_AttributeValue A
    WHERE A.AttributeValueID IN
    (
        SELECT AttributeValueID
        FROM TBL_AttributeValues
        WHERE AttributeValueID NOT IN (1319, 1320)  AND AttributeTypeID=1
    )
)

答案 1 :(得分:1)

SELECT  id
FROM    (
        SELECT  DISTINCT id
        FROM    REL_Aid_To_AttributeValue a
        WHERE   attributeValueId IN (1319, 1320)
        ) q
WHERE   NOT EXISTS
        (
        SELECT  id
        FROM    (
                SELECT  AttributeValueID
                FROM    TBL_AttributeValues
                WHERE   AttributeTypeID = 1
                        AND AttributeValueID NOT IN (1319, 1320)
                ) t
        JOIN    REL_Aid_To_AttributeValue a
        ON      a.id = q.id
                AND a.AttributeValueID = t.AttributeValueID
        )

答案 2 :(得分:0)

您想限制您的ID。在where子句的某处,您可以包含以下内容:

where id in (select distinct av.id
             from AttributeValueType avt join
                  AttributeValue av
                   on avt.AttributeValueId = av.AttributeValueId
             where avt.AttributeValueId in (1319, 1320) and avt.AttributeTypeID = 1
            )

如果您只想要那两个属性,请尝试:

Where id in (
    Select av.id
    From AttributeValueType avt join
         AttributeValue av
         on avt.AttributeValueId = av.AttributeValueId
    Group by av.id
    Having max(case when avt.AttributeValueId in (1319, 1320) and avt.AttributeTypeID = 1 then 0 else 1)=0
    Having max(case when 

答案 3 :(得分:0)

我认为你需要以某种方式添加'NOT EXISTS':

SELECT DISTINCT ID
FROM TBL_Aids x
WHERE AttributeValueID IN
(
    SELECT AttributeValueID
    FROM AttributeValueIDs
    WHERE AttributeValueID IN (1319, 1320) AND AttributeTypeID=1
) AND NOT EXISTS
(
    SELECT *
    FROM AttributeValueIDs a
    WHERE a.AttributeValueID NOT IN (1319, 1320) AND a.AttributeTypeID=1
    AND a.AttributeValueIDs = x.AttributeValueIDs
)

答案 4 :(得分:0)

您需要两次加入REL_Aid_To_AttributeValue以确保两个行存在

SELECT a.ID, a.Title, c.AttributeTypeID, 2 AS Total
FROM TBL_Aids a
join REL_Aid_To_AttributeValue b on a.ID=b.AidID and b.AttributeValueID = 1319
join REL_Aid_To_AttributeValue b2 on a.ID=b2.AidID and b2.AttributeValueID = 1320
join TBL_AttributeValues c on b.AttributeValueID=c.ID
WHERE a.Status=2
AND AttributeTypeID=1;

同样不是如果你知道你拥有什么价值,你知道它有多少,所以它是一个文字2

答案 5 :(得分:0)

您需要两次加入REL_Aid_To_AttributeValue以确保两个行存在

SELECT a.ID, a.Title, c.AttributeTypeID, 2 AS Total
FROM TBL_Aids a
join REL_Aid_To_AttributeValue b on a.ID=b.AidID and b.AttributeValueID = 1319
join REL_Aid_To_AttributeValue b2 on a.ID=b2.AidID and b2.AttributeValueID = 1320
join TBL_AttributeValues c on b.AttributeValueID=c.ID
WHERE a.Status=2
AND AttributeTypeID=1;

请注意,我已将您的查询转换为更现代的“join”语法。我建议你学习它。

另请注意,如果您知道自己拥有什么值,就会知道它们有多少,所以这是一个文字2

答案 6 :(得分:0)

感谢Quassnoi:

    SELECT ID, Title, Author
    FROM TBL_Aids X
    JOIN (
    SELECT AidID
    FROM    (
            SELECT  DISTINCT AidID
            FROM    REL_Aid_To_AttributeValue a
            WHERE   AttributeValueID IN (1319, 1320)
            ) q
    WHERE   NOT EXISTS
            (
            SELECT  AidID
            FROM    (
                    SELECT  ID
                    FROM    TBL_AttributeValues
                    WHERE   AttributeTypeID = 1
                            AND ID NOT IN (1319, 1320)
                    ) t
            JOIN    REL_Aid_To_AttributeValue a
            ON      a.AidID = q.AidID
                    AND a.AttributeValueID = t.ID
            ))
    Y ON X.ID=Y.AidID
    WHERE Status=2