我正在开发一个项目,在下一部分中,我需要比较 2 个查询的结果。
场景: 在一个表中我保留了球队中的所有球员。 另一个表中只有你教练要求参加比赛的人。
我想知道哪些玩家被排除在外
我采取的最佳方法是什么?
我可以使用类似的东西
(Query for Selecting all players)
EXCEPT
(Query for Selecting the ones called by the coach)
表格
所有玩家
Number | Name
------ | ------
23 | john
24 | Mario
选定的球员
Number | Name
------ | ------
23 | john
我希望它给出所选玩家表中缺少马里奥的结果
最佳答案
使用不存在
select * from all_players p1
where not exists
(select 1 from players p2
where p1.number=p2.number
and p1.name=p2.name
-- and -- You can add other columns here
) t
使用LEFT JOIN
select p1.* from all_players p1
left join players p2
on p1.number=p2.number
and p1.name=p2.name
-- and p1.last_name=p2.last_name --add other columns
where
(p2.number is null
and p2.name is null
-- and p2.last_name is null --add other columns
)
使用IN,如果有相同的key需要匹配
select * From all_players p
where number not in (select number from players)
关于C# MySQL - 2 个查询之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43718893/