【MySQL探索之旅】多表查询

马肤
这是懒羊羊

【MySQL探索之旅】多表查询,在这里插入图片描述,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,操作,进行,使用,第1张

📚博客主页:爱敲代码的小杨.

✨专栏:《Java SE语法》 | 《数据结构与算法》 | 《C生万物》 |《MySQL探索之旅》 |《Web世界探险家》

❤️感谢大家点赞👍🏻收藏⭐评论✍🏻,您的三连就是我持续更新的动力❤️

🙏小杨水平有限,欢迎各位大佬指点,相互学习进步!

【MySQL探索之旅】多表查询,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,操作,进行,使用,第2张

文章目录

  • 1. 多表查询的概念
    • 1.1 笛卡尔积
    • 2. 连接查询
      • 2.1 内连接
      • 2.2 外连接
        • 2.2.1 左外连接
        • 2.2.2 右外连接
        • 2.3 自连接
        • 3. 子查询
          • 3.1 单行子查询
          • 3.2 多行子查询
          • 4. 联合查询

            1. 多表查询的概念

            多表查询就是对多表查询我们需要的数据. 通过笛卡尔积进行查询

            1.1 笛卡尔积

            百度百科:

            笛卡尔乘积是指在数学中,两个集合X和Y的笛卡尓积(Cartesian product),又称直积,表示为X×Y,第一个对象是X的成员而第二个对象是Y的所有可能有序对的其中一个成员。

            【MySQL探索之旅】多表查询,image-20240416153106532,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,操作,进行,使用,第3张

            案例:

            【MySQL探索之旅】多表查询,image-20240416153733369,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,操作,进行,使用,第4张

            笛卡尔积就是得到了一个更大的表. 列数就是原来两个表列数的之和. 行数就是原来两个表行数之乘.

            【注意】:笛卡尔积是全排列的过程,在尝试穷举所有的可能性,自然就会产生一些不符合实际情况的数据

            上述案例就有一部分为无效数据/无意义的数据

            初始化数据:

            create table student(id int, name varchar(20), classId int);
            inset into student values (1,'张三',1);
            insert into student values (2,'李四',2);
            insert into student values (3,'赵五',3);
            insert into student values (4,'赵六',4);
            create table class(classId int, className varchar(20));
            insert into class values (1,'计算机科学1班');
            insert into class values (2,'软件工程1班');
            insert into class values (3,'人工智能1班');
            insert into class values (5,'信息安全1班');
            

            2. 连接查询

            2.1 内连接

            语法:

            -- 显示内连接
            select 字段 from 表1 别名1 [inner] join 表2 别名2 on 连接条件 and 其他条件;
            -- 隐式内连接
            select 字段 from 表1 别名1,表2 别名2 where 连接条件 and 其他条件;
            

            案例1:

            -- 查询张三的信息
            select * from student, class where student.classId = class.classId and name = '张三';
            -- 或者
            select * from student inner join class on student.classId = class.classId and name = '张三';
            

            运行结果:

            【MySQL探索之旅】多表查询,image-20240416160146712,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,操作,进行,使用,第5张

            案例2:

            -- 查询所有有班级的学生的信息
            select * from student inner join class on student.classId = class.classId;
            -- 或者
            select * from student, class where student.classId = class.classId;
            

            运行结果:

            【MySQL探索之旅】多表查询,image-20240416160536306,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,操作,进行,使用,第6张

            2.2 外连接

            外连接分为左外连接和右外连接。如果联合查询,左侧的表完全显示我们就说是左外连接;右侧的表完

            全显示我们就说是右外连接。

            2.2.1 左外连接

            【MySQL探索之旅】多表查询,image-20240416162417809,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,操作,进行,使用,第7张

            语法:

            select 字段名 from 表名1 left join 表名2 on 连接条件;
            

            案例:

            -- 查询所有同学的信息,没有班级也要显示
            select * from student left join class on student.classId = class.classId;
            

            运行结果:

            【MySQL探索之旅】多表查询,image-20240416161201950,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,操作,进行,使用,第8张

            2.2.2 右外连接

            【MySQL探索之旅】多表查询,image-20240416162556727,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,操作,进行,使用,第9张

            语法:

            select 字段名 from 表名1 right join 表名2 on 连接条件;
            

            案例:

            -- 查询所有班级的信息,没有学生的班级也要显示
            select * from student right join class on student.classId = class.classId;
            

            运行结果:

            【MySQL探索之旅】多表查询,image-20240416161341743,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,操作,进行,使用,第10张

            2.3 自连接

            自连接是指在同一张表连接自身进行查询。

            自连接将行与行之间的关系, 转换为列于列的关系

            测试表:

            【MySQL探索之旅】多表查询,image-20240418203459272,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,操作,进行,使用,第11张

            案例: 查询成绩表中的 Java 成绩大于 C语言成绩的同学

            【MySQL探索之旅】多表查询,image-20240418202430781,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,操作,进行,使用,第12张

            为什么直接自连接报错呢? 如何报错的呢? 通过别名的方式来进行自连接

            【MySQL探索之旅】多表查询,image-20240418202732384,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,操作,进行,使用,第13张

            添加连接条件

            select * from sore as s1,sore as s2 where s1.name = s2.name and s1.className ='Java' and s2.className = 'C语言' and s1.sore > s2.sore;
            

            运行结果:

            【MySQL探索之旅】多表查询,image-20240418203408939,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,操作,进行,使用,第14张

            3. 子查询

            子查询是指嵌入在其他 SQL 语句中的 select语句,也叫嵌套查询

            这种写法实际开发中需要慎重使用, 这种写法违背了编程基本的思想原则(化繁为简), 如果是合并之后的 SQL 命令仍然非常简单直观的话, 使用子查询也是可以的.

            3.1 单行子查询

            单行子查询: 返回一行记录的子查询

            案例: 查询张三同学的同班同学

            【MySQL探索之旅】多表查询,image-20240418204738020,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,操作,进行,使用,第15张

            上诉两条 SQL 命令就可以转化为一条命令

            select name from student where classId = (select classId from student where name = '张三') and name != '张三';
            

            运行结果:

            【MySQL探索之旅】多表查询,image-20240418204911546,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,操作,进行,使用,第16张

            3.2 多行子查询

            多行子查询:返回多行记录的子查询

            案例: 查询学习 计算机基础 或者 Python同学的课程信息

            【MySQL探索之旅】多表查询,在这里插入图片描述,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,操作,进行,使用,第17张

            上诉两条 SQL 命令就可以转化为一条命令

            select * from sore where name in (select name from sore where className = '计算机基础' or className = 'Python');
            

            运行结果:

            【MySQL探索之旅】多表查询,在这里插入图片描述,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,操作,进行,使用,第18张

            4. 联合查询

            在实际应用中,为了合并多个 select 的执行结果,可以使用集合操作符 union,union all。使用union 和union all时,前后查询的结果集中,字段需要一致。

            • union : 该操作符用于取得两个结果集的并集. 当使用该操作符时,会自动去掉结果集中的重复行。

              案例: 查询成绩小于90 或者 课程为 Java 的信息

              select * from sore where sore  

              运行结果:

              【MySQL探索之旅】多表查询,在这里插入图片描述,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,操作,进行,使用,第19张

            • union all : 该操作符用于取得两个结果集的并集。当使用该操作符时,不会去掉结果集中的重复行。

              案例: 查询成绩小于90 或者 课程为 Java 的信息

              select * from sore where sore  

              【MySQL探索之旅】多表查询,在这里插入图片描述,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,操作,进行,使用,第20张

              【MySQL探索之旅】多表查询,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,操作,进行,使用,第21张


文章版权声明:除非注明,否则均为VPS857原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复:表情:
评论列表 (暂无评论,0人围观)

还没有评论,来说两句吧...

目录[+]

取消
微信二维码
微信二维码
支付宝二维码