mysql 中的减法运算
minus
运算符在 sql 中用于查找表 a 中不存在于表 b 中的唯一元素。
通过本教程,我们将看到如何在 mysql 中模拟 minus
运算符以获得所需的结果。我们将通过使用 not in
、not exists
、left join
和 is null
来理解它。
我们还将看到每个提到的方法的语法,并探讨它们之间的一些差异。
在 mysql (8.0.27) 中模拟 minus
运算符
在 mysql 中使用 not in
让我们以学生-课程关系为例,在数据库中创建一个数据库和两个表。一个叫学生
,另一个叫 course
。
student
表有学生的 id
、first_name
、last_name
、gender
,course
表有 course_code
、course_title
和 student_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
表只有科学科目,我们想知道没有参加任何科学课程的学生。让我们填充表并查看 student
和 course
表。
示例代码:
#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 join
和 is null
示例代码:
select*fromstudentleftjoincourseonstudent.id=course.student_idwherecourse.student_idisnull;
输出:
在上述输出中,course_code
、course_title
和 student_id
显示为 null
,因为这些学生尚未注册或注册任何课程。
你还可以将 id
、first_name
、last_name
和 gender
列与其他方法的输出进行比较,以查看一切是否按预期工作。
何时使用 not in
、not exists
和 left join
/is null
现在的问题是如何选择这三种方法中的一种。你可以根据几个基本的点来决定。
- 这三种方法的主要区别在于
not in
和not exists
仅显示左表(第一个选择查询)中的值。 - 但是
left join
/is null
将输出左表以及null
值而不是右表的值在左右表之间找不到匹配项的情况下,因为left join
/is null
用于从多个表中检索数据。 - 另一个区别是它们如何处理右表中的
null
值,因为left join
/is null
和not exists
在语义上是等价的,而not in
不是。 - 如果在右表中没有找到满足相等条件的行,
not exists
返回true
。 not in
的行为不同;如果在列表中找到一行,in
将输出true
,然后not in
将返回false
。另一方面,如果在列表中没有找到一行,那么in
将返回null
,并且not in
也将返回null
,因为对null
的否定是null
。
结论
考虑到上面讨论的所有细节,我们得出的结论是 mysql 不支持 minus
操作,但还有其他方法可以模拟 minus
操作。你可以根据自己的需要、舒适度和要求使用它。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
如何在 mysql 中声明和使用变量
发布时间:2024/03/26 浏览次数:115 分类:mysql
-
当你需要在 mysql 中的脚本中存储单个值时,最好的方法是使用变量。变量有不同的种类,有必要知道何时以及如何使用每种类型。
发布时间:2024/03/26 浏览次数:176 分类:mysql
-
本教程演示了如何在 mysql 中重置自动增量。
在 mysql 中使用 mysqladmin 刷新主机解除阻塞
发布时间:2024/03/26 浏览次数:82 分类:mysql
-
你将了解阻止主机的原因。此外,通过使用 phpmyadmin 和命令提示符刷新主机缓存来解除阻塞的不同方法和效果。
发布时间:2024/03/26 浏览次数:199 分类:mysql
-
本教程演示如何在 mysql 中转换为整数。