C# MySQL - 2 个查询之间的差异

标签 c# mysql sql

我正在开发一个项目,在下一部分中,我需要比较 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/

相关文章:

c# - 系统反射 .Invoke() 无法捕获异步调用方法内的异常

php - 如何在 MySQL 中通过一次选择为每个 'category' 获取 5 条记录?

sql - SQL 中的 INSERT 与 INSERT INTO

sql - 获取每个项目的最新价格

c# - 如何使用 dapper 获取第一个子值(通常查询 count(*) 作为唯一结果)

c# - 是否可以创建一个可以同时作为独立应用程序和 IIS 内部托管的 OWIN 应用程序?

C#/XNA 对比。 java/vector 用于简单的游戏开发

mysql - 从条件为 x 的同一个表中检索数据 mysql

mysql - 什么网络框架提供基于表关系和外键的自动数据库脚手架?

c# - ServiceStack 中是否有任何等效于 ValidateAntiForgeryToken 的东西?