如何从这个mssql / php查询中排除一些东西?

时间:2013-09-26 23:27:31

标签: php sql-server

我有这个:

SELECT TOP 100 
    IDNum, 
    IDName, 
    Nation, 
    (SELECT 
        SUM(Loyalty) 
    FROM 
        USERDATA 
    WHERE 
        USERDATA.Knights = KNIGHTS.IDNum 
        AND USERDATA.Authority IN(1, 2)
    ) as ClanLoyalty 
FROM 
    KNIGHTS 
ORDER BY 
    ClanLoyalty DESC

但我想要排除Knights (21 and 25),以便在该查询中包含骑士IDNum 21和25 WONT?

如何排除它们?

1 个答案:

答案 0 :(得分:0)

这不是外部查询的简单where子句吗?

SELECT TOP 100 
    IDNum, 
    IDName, 
    Nation, 
    (SELECT 
        SUM(Loyalty) 
    FROM 
        USERDATA 
    WHERE 
        USERDATA.Knights = KNIGHTS.IDNum 
        AND USERDATA.Authority IN(1, 2)
    ) as ClanLoyalty 
FROM 
    KNIGHTS 
where
    knights.idnum not in (21,22)
ORDER BY 
    ClanLoyalty DESC

如果从主查询中排除它们,它们就不会在内部查询上匹配,因为无论如何它们都会加入IDNum字段。

在这方面,您可能还需要使用read of this Q&A that I put together来覆盖很多像这样的SQL。