mysql 中的减法运算-ag捕鱼王app官网

mysql 中的减法运算

作者:迹忆客 最近更新:2024/03/25 浏览次数:

minus 运算符在 sql 中用于查找表 a 中不存在于表 b 中的唯一元素。

通过本教程,我们将看到如何在 mysql 中模拟 minus 运算符以获得所需的结果。我们将通过使用 not innot existsleft joinis null 来理解它。

我们还将看到每个提到的方法的语法,并探讨它们之间的一些差异。


在 mysql (8.0.27) 中模拟 minus 运算符

在 mysql 中使用 not in

让我们以学生-课程关系为例,在数据库中创建一个数据库和两个表。一个叫学生,另一个叫 course

student 表有学生的 idfirst_namelast_namegendercourse 表有 course_codecourse_titlestudent_id 列。

示例代码:

/*
create the database and use it for table creation and other manipulation*/createschemadb_practice_minus_operator;usedb_practice_minus_operator;#createstudenttablecreatetabledb_practice_minus_operator.student(idintauto_incrementprimarykey,first_namevarchar(64)notnull,last_namevarchar(64)notnull,gendervarchar(30)notnull);#createcoursetablecreatetablecourse(course_codevarchar(60)notnull,course_titlevarchar(64)notnull,student_idintnotnull,primarykey(course_code),foreignkey(student_id)referencesstudent(id));

请记住,course 表只有科学科目,我们想知道没有参加任何科学课程的学生。让我们填充表并查看 studentcourse 表。

示例代码:

#populatestudenttableinsertintostudent(first_name,last_name,gender)values('shaajeel','daniel','male'),('nayya','preston','female'),('james','robert','male'),('jennifer','john','female'),('sarah','paul','female'),('karen','donald','female'),('thomas','christopher','male'),('lisa','mark','female'),('anthony','richard','male'),('matthew','charles','male');#populatecoursetableinsertintocourse(course_code,course_title,student_id)values(125854,'biology',1),(542968,'mathematics',2),(125648,'computer science',5),(654891,'physics',4),(483215,'chemistry',8),(147934,'artificial intelligence',6);

两个表中的当前数据如下所示。

学生桌:

课程表:

现在,编写以下 sql 查询来了解 mysql 中的 minus 操作模拟。

示例代码:

#simulateminusoperatorinmysqlselect*fromstudentwherestudent.idnotin(selectstudent_idfromcourse);

输出:

在 mysql 中使用 not exists

示例代码:

#simulateminusoperatorinmysqlselect*fromstudentstdwherenotexists(selectstudent_idfromcoursewherestd.id=student_id);

输出:

在 mysql 中使用 left joinis null

示例代码:

select*fromstudentleftjoincourseonstudent.id=course.student_idwherecourse.student_idisnull;

输出:

在上述输出中,course_codecourse_titlestudent_id 显示为 null,因为这些学生尚未注册或注册任何课程。

你还可以将 idfirst_namelast_namegender 列与其他方法的输出进行比较,以查看一切是否按预期工作。


何时使用 not innot existsleft join/is null

现在的问题是如何选择这三种方法中的一种。你可以根据几个基本的点来决定。

  • 这三种方法的主要区别在于 not innot exists 仅显示左表(第一个选择查询)中的值。
  • 但是 left join/is null 将输出左表以及 null 值而不是右表的值在左右表之间找不到匹配项的情况下,因为 left join/ is null 用于从多个表中检索数据。
  • 另一个区别是它们如何处理右表中的 null 值,因为 left join/is nullnot exists 在语义上是等价的,而 not in 不是。
  • 如果在右表中没有找到满足相等条件的行,not exists 返回 true
  • not in 的行为不同;如果在列表中找到一行,in 将输出 true,然后 not in 将返回 false。另一方面,如果在列表中没有找到一行,那么 in 将返回 null,并且 not in 也将返回 null,因为对 null 的否定是 null

结论

考虑到上面讨论的所有细节,我们得出的结论是 mysql 不支持 minus 操作,但还有其他方法可以模拟 minus 操作。你可以根据自己的需要、舒适度和要求使用它。

上一篇:

下一篇:mysql 中的转义序列

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

如何在 mysql 中声明和使用变量

发布时间:2024/03/26 浏览次数:115 分类:mysql

当你需要在 mysql 中的脚本中存储单个值时,最好的方法是使用变量。变量有不同的种类,有必要知道何时以及如何使用每种类型。

发布时间:2024/03/26 浏览次数:176 分类:mysql

本教程演示了如何在 mysql 中重置自动增量。

在 mysql 中实现刷新权限

发布时间:2024/03/26 浏览次数:211 分类:mysql

本教程介绍了 mysql 中的刷新权限命令,用于刷新授权表并影响允许的更改。

在 mysql 中设置时区

发布时间:2024/03/26 浏览次数:93 分类:mysql

在本教程中,我们将学习如何在 mysql 服务器中更改时区。

发布时间:2024/03/26 浏览次数:199 分类:mysql

本教程演示如何在 mysql 中转换为整数。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图