mysql查询笔试题_mysql笔试题大餐---2、exists加一些查询

2023-10-18 13:50

本文主要是介绍mysql查询笔试题_mysql笔试题大餐---2、exists加一些查询,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

mysql笔试题大餐---2、exists加一些查询

一、总结

一句话总结:

实践:我之前的mysql真的学的太浅了,这种情况下,依据实践(做题)才是唯一能把它学好的方式

1、MySQL中EXISTS如何使用?

1、exists是和in相提并论的东西

2、返回值bool值:EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或False

比如在Northwind数据库中有一个查询为SELECT c.CustomerId,CompanyName FROM Customers c WHERE EXISTS(SELECT OrderID FROM Orders o WHERE o.CustomerID=c.CustomerID)

这里面的EXISTS是如何运作呢?子查询返回的是OrderId字段,可是外面的查询要找的是CustomerID和CompanyName字段,这两个字段肯定不在OrderID里面啊,这是如何匹配的呢?

EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或FalseEXISTS指定一个子查询,检测 行 的存在。

语法:EXISTSsubquery

参数: subquery 是一个受限的SELECT 语句 (不允许有 COMPUTE 子句和 INTO关键字)。

结果类型: Boolean 如果子查询包含行,则返回 TRUE ,否则返回 FLASE 。

2、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名?

注意exists:select Student.S#,Student.Sname from Student,SC where Student.S#=SC.S# and SC.C#='001'and exists( Select * from SC as SC_2 where SC_2.S#=SC.S# and SC_2.C#='002');

EXISTS()查询是将主查询的结果集放到子查询中做验证,根据验证结果是true或false来决定主查询数据结果是否得以保存。

3、mysql中的增删改查?

用脑子:站在设计者的思想上怎么才能完成任务:INSERT INTO student(id,name,grade) VALUES(1,'zhangshan',98);

删:DELETE  FROM student WHERE id=7;

改:UPDATE student SET name=‘caocao’,grade=50 WHERE id=1;

4、查询所有同学的学号、姓名、选课数、总成绩?

多表查询推荐用链接查询方式:比如inner join

##多表查询推荐用链接查询方式:比如inner join

select s.s_id,s.s_name,count(ss.ss_s_id) course_num,sum(ss.ss_score) from student s inner join student_score ss on s.s_id=ss.ss_s_id group by ss.ss_s_id;

5、查询姓“鲁”的老师的个数?

like中的百分号的用法 + count用法

select count(t.t_id) num from teacher t where t_name like '鲁%'

6、查询没学过“鲁迅”老师课的同学的学号、姓名?

没学过:总的减去学过了的

去掉重复:distinct函数

总的减去学过了的如何实现:通过嵌套查询+ not in

##找出鲁迅老师课的id,学生not in里面就好

##没学过:总的减去学过了的

##去掉重复:distinct函数

##总的减去学过了的如何实现:通过嵌套查询+ not in

select c.c_id from course c inner join teacher t on t.t_id=c.c_t_id where t_name='鲁迅'

select distinct(c.c_id) from course c inner join teacher t on t.t_id=c.c_t_id where t_name='鲁迅'

select distinct(s_id) from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id inner join teacher t on t.t_id=c.c_t_id where c.c_id in (select distinct(c.c_id) from course c inner join teacher t on t.t_id=c.c_t_id where t_name='鲁迅')select s.s_id,s.s_name from student s where s.s_id not in (select distinct(s_id) from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id inner join teacher t on t.t_id=c.c_t_id where c.c_id in (select distinct(c.c_id) from course c inner join teacher t on t.t_id=c.c_t_id where t_name='鲁迅'))

7、查询学过“鲁迅作品集”并且也学过“狂人日记”课程的同学的学号、姓名?

思路:把学过“鲁迅作品集”的人的id找出来,把学过“狂人日记”的人的id找出来,做对比找出同时学过的人的id

特别注意:from的表示一个嵌套查询的表:from (select s.s_id from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id where c.c_name='鲁迅作品集')  a

##思路:把学过“鲁迅作品集”的人的id找出来,把学过“狂人日记”的人的id找出来,做对比找出同时学过的人的id

##特别注意:from的表示一个嵌套查询的表:from (select s.s_id from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id where c.c_name='鲁迅作品集') aselect * from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id inner join teacher t on t.t_id=c.c_t_id

#把学过“鲁迅作品集”的人的id找出来select s.s_id from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id where c.c_name='鲁迅作品集'#把学过“狂人日记”的人的id找出来select s.s_id from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id where c.c_name='狂人日记'#做对比select a.s_id from (select s.s_id from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id where c.c_name='鲁迅作品集') a,(select s.s_id from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id where c.c_name='狂人日记') b where a.s_id=b.s_id

#找学生信息select * from student where s_id in (select a.s_id from (select s.s_id from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id where c.c_name='鲁迅作品集') a,(select s.s_id from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id where c.c_name='狂人日记') b where a.s_id=b.s_id)

二、内容在总结中

1、具体操作及截图

数据库数据的大致样子:

select * from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id inner join teacher t on t.t_id=c.c_t_id

ca5d149fff0f750a9f94a0ff2b29b828.png

#3、查询所有同学的学号、姓名、选课数、总成绩;

#3、查询所有同学的学号、姓名、选课数、总成绩;

##多表查询推荐用链接查询方式:比如innerjoin

select s.s_id,s.s_name,count(ss.ss_s_id) course_num,sum(ss.ss_score) from student s inner join student_score ss on s.s_id=ss.ss_s_id group by ss.ss_s_id;

4b274f725dacff067185dee049ffdc51.png

#4、查询姓“鲁”的老师的个数;

select count(t.t_id) num from teacher t where t_name like '鲁%'

842ec195c211f7deae07023798f72431.png

#5、查询没学过“鲁迅”老师课的同学的学号、姓名;

##找出鲁迅老师课的id,学生not in里面就好

##没学过:总的减去学过了的

##去掉重复:distinct函数

##总的减去学过了的如何实现:通过嵌套查询+ not in

select c.c_id from course c inner join teacher t on t.t_id=c.c_t_id where t_name='鲁迅'

select distinct(c.c_id) from course c inner join teacher t on t.t_id=c.c_t_id where t_name='鲁迅'

select distinct(s_id) from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id inner join teacher t on t.t_id=c.c_t_id where c.c_id in (select distinct(c.c_id) from course c inner join teacher t on t.t_id=c.c_t_id where t_name='鲁迅')select s.s_id,s.s_name from student s where s.s_id not in (select distinct(s_id) from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id inner join teacher t on t.t_id=c.c_t_id where c.c_id in (select distinct(c.c_id) from course c inner join teacher t on t.t_id=c.c_t_id where t_name='鲁迅'))

12611e536d89c434b57c466c8bf08a43.png

#6、查询学过“鲁迅作品集”并且也学过“狂人日记”课程的同学的学号、姓名;

##思路:把学过“鲁迅作品集”的人的id找出来,把学过“狂人日记”的人的id找出来,做对比找出同时学过的人的id

##特别注意:from的表示一个嵌套查询的表:from (select s.s_id from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id where c.c_name='鲁迅作品集') aselect * from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id inner join teacher t on t.t_id=c.c_t_id

#把学过“鲁迅作品集”的人的id找出来select s.s_id from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id where c.c_name='鲁迅作品集'#把学过“狂人日记”的人的id找出来select s.s_id from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id where c.c_name='狂人日记'#做对比select a.s_id from (select s.s_id from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id where c.c_name='鲁迅作品集') a,(select s.s_id from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id where c.c_name='狂人日记') b where a.s_id=b.s_id

#找学生信息select * from student where s_id in (select a.s_id from (select s.s_id from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id where c.c_name='鲁迅作品集') a,(select s.s_id from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id where c.c_name='狂人日记') b where a.s_id=b.s_id)

03fa53b2032ffaccb67085fd4a45aad8.png

2、代码

#3、查询所有同学的学号、姓名、选课数、总成绩;

##多表查询推荐用链接查询方式:比如innerjoin

select s.s_id,s.s_name,count(ss.ss_s_id) course_num,sum(ss.ss_score) from student s inner join student_score ss on s.s_id=ss.ss_s_id group byss.ss_s_id;

#4、查询姓“鲁”的老师的个数;select count(t.t_id) num from teacher t where t_name like '鲁%'#5、查询没学过“鲁迅”老师课的同学的学号、姓名;

##找出鲁迅老师课的id,学生notin里面就好

##没学过:总的减去学过了的

##去掉重复:distinct函数

##总的减去学过了的如何实现:通过嵌套查询+ not in

select c.c_id from course c inner join teacher t on t.t_id=c.c_t_id where t_name='鲁迅'

select distinct(c.c_id) from course c inner join teacher t on t.t_id=c.c_t_id where t_name='鲁迅'

select distinct(s_id) from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id inner join teacher t on t.t_id=c.c_t_id where c.c_id in (select distinct(c.c_id) from course c inner join teacher t on t.t_id=c.c_t_id where t_name='鲁迅')select s.s_id,s.s_name from student s where s.s_id not in (select distinct(s_id) from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id inner join teacher t on t.t_id=c.c_t_id where c.c_id in (select distinct(c.c_id) from course c inner join teacher t on t.t_id=c.c_t_id where t_name='鲁迅'))

#6、查询学过“鲁迅作品集”并且也学过“狂人日记”课程的同学的学号、姓名;

##思路:把学过“鲁迅作品集”的人的id找出来,把学过“狂人日记”的人的id找出来,做对比找出同时学过的人的id

##特别注意:from的表示一个嵌套查询的表:from (select s.s_id from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id where c.c_name='鲁迅作品集') aselect * from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id inner join teacher t on t.t_id=c.c_t_id

#把学过“鲁迅作品集”的人的id找出来select s.s_id from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id where c.c_name='鲁迅作品集'#把学过“狂人日记”的人的id找出来select s.s_id from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id where c.c_name='狂人日记'#做对比select a.s_id from (select s.s_id from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id where c.c_name='鲁迅作品集') a,(select s.s_id from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id where c.c_name='狂人日记') b where a.s_id=b.s_id

#找学生信息select * from student where s_id in (select a.s_id from (select s.s_id from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id where c.c_name='鲁迅作品集') a,(select s.s_id from student s inner join student_score ss on s.s_id=ss.ss_s_id inner join course c on c.c_id=ss.ss_c_id where c.c_name='狂人日记') b where a.s_id=b.s_id)

这篇关于mysql查询笔试题_mysql笔试题大餐---2、exists加一些查询的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/232977

相关文章

SQL注入漏洞扫描之sqlmap详解

《SQL注入漏洞扫描之sqlmap详解》SQLMap是一款自动执行SQL注入的审计工具,支持多种SQL注入技术,包括布尔型盲注、时间型盲注、报错型注入、联合查询注入和堆叠查询注入... 目录what支持类型how---less-1为例1.检测网站是否存在sql注入漏洞的注入点2.列举可用数据库3.列举数据库

Oracle查询优化之高效实现仅查询前10条记录的方法与实践

《Oracle查询优化之高效实现仅查询前10条记录的方法与实践》:本文主要介绍Oracle查询优化之高效实现仅查询前10条记录的相关资料,包括使用ROWNUM、ROW_NUMBER()函数、FET... 目录1. 使用 ROWNUM 查询2. 使用 ROW_NUMBER() 函数3. 使用 FETCH FI

数据库oracle用户密码过期查询及解决方案

《数据库oracle用户密码过期查询及解决方案》:本文主要介绍如何处理ORACLE数据库用户密码过期和修改密码期限的问题,包括创建用户、赋予权限、修改密码、解锁用户和设置密码期限,文中通过代码介绍... 目录前言一、创建用户、赋予权限、修改密码、解锁用户和设置期限二、查询用户密码期限和过期后的修改1.查询用

Mysql虚拟列的使用场景

《Mysql虚拟列的使用场景》MySQL虚拟列是一种在查询时动态生成的特殊列,它不占用存储空间,可以提高查询效率和数据处理便利性,本文给大家介绍Mysql虚拟列的相关知识,感兴趣的朋友一起看看吧... 目录1. 介绍mysql虚拟列1.1 定义和作用1.2 虚拟列与普通列的区别2. MySQL虚拟列的类型2

mysql数据库分区的使用

《mysql数据库分区的使用》MySQL分区技术通过将大表分割成多个较小片段,提高查询性能、管理效率和数据存储效率,本文就来介绍一下mysql数据库分区的使用,感兴趣的可以了解一下... 目录【一】分区的基本概念【1】物理存储与逻辑分割【2】查询性能提升【3】数据管理与维护【4】扩展性与并行处理【二】分区的

MySQL中时区参数time_zone解读

《MySQL中时区参数time_zone解读》MySQL时区参数time_zone用于控制系统函数和字段的DEFAULTCURRENT_TIMESTAMP属性,修改时区可能会影响timestamp类型... 目录前言1.时区参数影响2.如何设置3.字段类型选择总结前言mysql 时区参数 time_zon

Python MySQL如何通过Binlog获取变更记录恢复数据

《PythonMySQL如何通过Binlog获取变更记录恢复数据》本文介绍了如何使用Python和pymysqlreplication库通过MySQL的二进制日志(Binlog)获取数据库的变更记录... 目录python mysql通过Binlog获取变更记录恢复数据1.安装pymysqlreplicat

使用SQL语言查询多个Excel表格的操作方法

《使用SQL语言查询多个Excel表格的操作方法》本文介绍了如何使用SQL语言查询多个Excel表格,通过将所有Excel表格放入一个.xlsx文件中,并使用pandas和pandasql库进行读取和... 目录如何用SQL语言查询多个Excel表格如何使用sql查询excel内容1. 简介2. 实现思路3

Mysql DATETIME 毫秒坑的解决

《MysqlDATETIME毫秒坑的解决》本文主要介绍了MysqlDATETIME毫秒坑的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着... 今天写代码突发一个诡异的 bug,代码逻辑大概如下。1. 新增退款单记录boolean save = s

mysql-8.0.30压缩包版安装和配置MySQL环境过程

《mysql-8.0.30压缩包版安装和配置MySQL环境过程》该文章介绍了如何在Windows系统中下载、安装和配置MySQL数据库,包括下载地址、解压文件、创建和配置my.ini文件、设置环境变量... 目录压缩包安装配置下载配置环境变量下载和初始化总结压缩包安装配置下载下载地址:https://d