SQl查询,用于查找用户尚未预测但其他用户可能已经预测的多个fixture



我有一个足球预测数据库,我需要一个sql查询,我可以使用它来显示特定用户尚未预测的所有fixture。所有的预测都保存在UserPredictions表中,所以如果没有其他条件,左连接检查FixtureID是否为空将无法工作。

我有一个固定装置表fixturid, HomeTeam, AwayTeam &FixtureStartTime以及UserPredictionID, UserID, FixtureID, HomeTeamPrediction &AwayTeamPrediction .

例如:一共有4个夹具

用户1已经预测了fixture 1 &2 .

用户2只预测了夹具2。

用户3已经预测了夹具1-4。

我想获得用户1尚未预测的固定装置(3 &4),但我只想能够选择UserID,查询将显示基于用户的杰出预测。

我无法理解它。由于

使用not exists:

select f.*
from fixtures f
where not exists (select 1
from predictions p
where p.FixtureID = f.FixtureID and
p.UserId = ?
);

?是您关心的用户的参数占位符。

试试这个:

编辑:

select * from fixtures
where fixture_id NOT IN (select fixture_id from UserPredictions where user_id = 1) 

最新更新