0

我有一个类似于以下的表:

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 或(含)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 的总数。我是否需要某种子查询来完成此操作?任何帮助,将不胜感激!非常感谢。

4

7 回答 7

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
    )
)
于 2012-05-22T19:55:19.217 回答
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
        )
于 2012-05-22T20:30:14.373 回答
0

你想限制你的身份。在 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 
于 2012-05-22T19:56:47.537 回答
0

我认为您需要以某种方式添加“不存在”:

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
)
于 2012-05-22T19:58:46.323 回答
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.

于 2012-05-22T20:00:57.080 回答
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.

于 2012-05-22T20:02:42.580 回答
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
于 2012-05-23T18:32:40.410 回答