简体   繁体   中英

How to exclude something from this mssql/php query?

I've got this:

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

but I want to exclude Knights (21 and 25) so knights IDNum 21 and 25 WONT be included in that query ?

How to exclude them ?

Wouldn't this be a simple where clause on your outer query?

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

If you exclude it from the main query, they won't be matched on the inner query either as you have them joining on the IDNum field anyhow.

On that note, you might also want to take a read of this Q&A that I put together which covers off a lot of SQL like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM