如果所有fk_winnerid都为null,我将如何才能检索此SQL语句?

时间:2013-09-05 18:39:07

标签: mysql sql sql-server

以下是我的发言:

select TournamentID, TournamentDateTime, TournamentName, TournamentTypeName, BossTournamentID 
from vwTournaments 
where tournamentid in 
(
    select distinct(fk_tournamentid) 
    from tournamentprizes 
    where place between 1 
    and (select max(numranks) from tournaments) 
    and fk_winnerid is not null
) 
and TournamentDateTime > '8/29/2013' 
and 
(
    TournamentID like '%%' 
    or TournamentDateTime like '%%' 
    or BossTournamentID like '%%' 
    or TournamentTypeName like '%%' 
    or TournamentName like '%%'
) 
order by TournamentDateTime DESC

需要修复的部分是“并且fk_winnerid不为空”“我想要说明所有锦标赛奖品是否有非空的fk_winnerid。不只是一个奖项。

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您需要一个相关的EXISTS子查询:

from vwTournaments AS t
where NOT EXISTS 
(
    select 1 
    from tournamentprizes 
    where fk_tournamentid = t.tournamentid 
    and place between 1 
                  and (select max(numranks) from tournaments) 
    and fk_winnerid IS NULL
) 
and TournamentDateTime > '20130829' 
and ...