当前位置: 首页 > news >正文

五、多表连接查询——内连接

五、多表连接查询——内连接

1.什么是内连接

根据相同的某列把多个表合并。

 

2.等值连接【最常用

使用等于号(=)运算符比较被连接列的列值。

(1)等值连接语法

<1>格式一:

select 表名.列名
from 表名1 [inner] join 表名2
on 表名1.列名=表名2.列名 [...n]

<2>格式二:

select 表名.列名
from 表名1,表名2 [,...]
where 表名1.列名=表名2.列名 [and...]

(2)将学生表(student)和成绩表(student_score)通过学号(student_id)连接起来。【选择全部数据

<1>使用inner join的方法

select * from student inner join student_score on student.student_id=student_score.student_id;

<2>使用where

select * from student,student_score where student.student_id=student_score.student_id;

(2)将学生表(student)和成绩表(student_score)通过学号(student_id)连接起来。【选择学号、姓名、成绩三列

select student.student_id,student.name,student_score.score from student join student_score on student.student_id=student_score.student_id;

3.不等值连接

在连接条件使用除了等于号(=)之外的其它比较运算符比较被连接的列的列值。

比如:>、>=、<、<=、!=、<>、!>、!<。

(1)示例:将学生表(student)和成绩表(student_score)通过学号(student_id)使用>连接起来。【选择全部数据

select * from student join student_score on student.student_id>student_score.student_id;

4.自然连接

自动去除重复的属性列,仅保留所有不重复的属性列。

自然连接中无需指定连接条件,自动匹配列值相同的字段。

(1)示例:将学生表(student)和成绩表(student_score)自然连接。【选择全部数据

select * from student natural join student_score;

(2)示例:将学生表(student)和成绩表(student_score)自然连接。【选择部分数据

select student.student_id,student.name,student_score.student_id,student_score.score 
from student natural join student_score;

为什么会有重复值?

虽然说select先写,但是select的操作在这个sql语句中最后执行,首先执行自然连接,然后执行选择数据。在自然连接时得到了一张表,但是这张表的结果被select的操作给覆盖掉了。

相关文章:

  • Exchange安装简介
  • 五、多表连接查询——外连接
  • Windows Update技巧与全故障解决(转贴)
  • 五、多表连接查询——聚合函数
  • 【强烈推荐】Windows 更新疑难解答(转贴)
  • 五、多表连接查询——数据分组
  • Oracle9i新特性-索引监视及注意事项[修正版]
  • 五、多表连接查询——对分组结果进行过滤
  • MSN Messenger无法登陆
  • 六、子查询——介绍
  • 上海ACM/ICPC归来
  • 六、子查询——简单子查询
  • 软件教育杂感
  • 六、子查询——多行子查询
  • linux下Anaconda的虚拟环境
  • #Java异常处理
  • [微信小程序] 使用ES6特性Class后出现编译异常
  • 【407天】跃迁之路——程序员高效学习方法论探索系列(实验阶段164-2018.03.19)...
  • 【挥舞JS】JS实现继承,封装一个extends方法
  • 【跃迁之路】【477天】刻意练习系列236(2018.05.28)
  • Android开源项目规范总结
  • PHP 7 修改了什么呢 -- 2
  • Swoft 源码剖析 - 代码自动更新机制
  • Vue2 SSR 的优化之旅
  • 关于for循环的简单归纳
  • 融云开发漫谈:你是否了解Go语言并发编程的第一要义?
  • 如何优雅地使用 Sublime Text
  • 如何正确配置 Ubuntu 14.04 服务器?
  • 深度学习中的信息论知识详解
  • 算法系列——算法入门之递归分而治之思想的实现
  • 小而合理的前端理论:rscss和rsjs
  • 一起来学SpringBoot | 第十篇:使用Spring Cache集成Redis
  • 最简单的无缝轮播
  • hi-nginx-1.3.4编译安装
  • ​Linux·i2c驱动架构​
  • ​软考-高级-信息系统项目管理师教程 第四版【第19章-配置与变更管理-思维导图】​
  • ###STL(标准模板库)
  • $分析了六十多年间100万字的政府工作报告,我看到了这样的变迁
  • (附源码)计算机毕业设计ssm基于B_S的汽车售后服务管理系统
  • (免费领源码)python#django#mysql校园校园宿舍管理系统84831-计算机毕业设计项目选题推荐
  • (十五)devops持续集成开发——jenkins流水线构建策略配置及触发器的使用
  • (一)基于IDEA的JAVA基础1
  • (转)项目管理杂谈-我所期望的新人
  • (转载)(官方)UE4--图像编程----着色器开发
  • .【机器学习】隐马尔可夫模型(Hidden Markov Model,HMM)
  • .net mvc actionresult 返回字符串_.NET架构师知识普及
  • .NET连接数据库方式
  • .net中调用windows performance记录性能信息
  • /run/containerd/containerd.sock connect: connection refused
  • /var/lib/dpkg/lock 锁定问题
  • @Autowired和@Resource的区别
  • @Builder用法
  • @param注解什么意思_9000字,通俗易懂的讲解下Java注解
  • [《百万宝贝》观后]To be or not to be?
  • [16/N]论得趣