如何选择其他表中没有链接记录的记录为空?

oipij1gg  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(242)

我有以下sql:

SELECT tournaments_blg.tournament_id_blg
FROM matches_blg  LEFT JOIN tournaments_blg ON tournaments_blg.tournament_id_blg = matches_blg.tournament_id_blg
WHERE matches_blg.match_id_op IS NULL
GROUP BY tournaments_blg.tournament_id_blg

这将选择所有 tournaments_blg.tournament_id_blg 其中任何一个 matches_blg.match_id_opNULL .
我怎么只选择全部 tournaments_blg.tournament_id_blg 如果所有的 matches_blg.match_id_opNULL ?

u2nhd7ah

u2nhd7ah1#

SELECT tb.tournament_id_blg
FROM tournaments_blg tb
LEFT JOIN matches_blg mb ON tb.tournament_id_blg = mb.tournament_id_blg 
                        AND mb.match_id_op IS NOT NULL
WHERE mb.tournament_id_blg IS NULL

SELECT tb.tournament_id_blg
FROM tournaments_blg tb
WHERE NOT EXISTS ( SELECT NULL
                   FROM matches_blg mb 
                   WHERE tb.tournament_id_blg = mb.tournament_id_blg 
                     AND mb.match_id_op IS NOT NULL )
goucqfw6

goucqfw62#

试试这个: SELECT tournaments_blg.tournament_id_blg FROM matches_blg WHERE matches_blg.match_id_op NOT IN (Select tournaments_blg from tournaments_blg )

相关问题