SQL连接查询
连接查询:通过连接运算符可以实现多个表查询。连接是关系数据库模型的主要特点,也是它区别于其它类型数据库管理系统的一个标志。
常用的两个链接运算符:
1.join on
2.union
在关系数据库管理系统中,表建立时各数据之间的关系不必确定,常把一个实体的所有信息存放在一个表中。当检索数据时,通过连接操作查询出存放在多个表中的不同实体的信息。连接操作给用户带来很大的灵活性,他们可以在任何时候增加新的数据类型。为不同实体创建新的表,随后通过连接进行查询。
示例一:

示例二:

示例三:

示例四:

示例五:

示例六:

示例七:

示例八:

练习:
create database lianxi119
go
use lianxi119
go
create table score
(
code int primary key identity(1001,1),
yu decimal(10,2),
shu decimal(10,2),
ying decimal(10,2),
)
go
create table Student
(
scode int primary key identity(1001,1),
sname varchar(10),
banji varchar(10),
yujiao int,
shujiao int,
yingjiao int,
)
go
create table Teacher
(
tcode int primary key identity(1,1),
tname varchar(10),
tkemu varchar(10),
)
go insert into score values(99,88,77)
insert into score values(88,77,66)
insert into score values(77,66,55)
insert into score values(66,55,44)
insert into score values(55,44,33)
insert into score values(89,79,69)
insert into score values(79,69,59)
insert into score values(59,49,39) select *from score insert into Student values('A','一班',2,4,8)
insert into Student values('B','一班',1,5,7)
insert into Student values('C','二班',3,6,9)
insert into Student values('D','二班',2,4,8)
insert into Student values('E','三班',1,5,7)
insert into Student values('F','三班',3,6,9)
insert into Student values('G','四班',2,4,7)
insert into Student values('H','四班',1,5,9) select *from Student insert into Teacher values('玲玲','语文')
insert into Teacher values('凌凌','语文')
insert into Teacher values('灵灵','语文')
insert into Teacher values('铃铃','数学')
insert into Teacher values('媛媛','数学')
insert into Teacher values('元元','数学')
insert into Teacher values('圆圆','英语')
insert into Teacher values('方方','英语')
insert into Teacher values('芳芳','英语') select *from Teacher --查询此次语文考试成绩最高的学生
select *from Student where scode =(select code from score where yu =(select MAX(yu)from score))
--查询此次数学考试成绩最低的学生的任课教师的信息
select *from Teacher where tcode =(select shujiao from Student where scode =(select code from score where shu=(select MAX(shu)from score)))
--查询汇总成一个表:各门课分数、学生姓名、班级、任课老师的信息
select Student.sname,score.shu,score.ying,score.yu,(select tname from Teacher where tcode = Student.shujiao)as 数学老师,
(select tname from Teacher where tcode = Student.yingjiao)as 英语老师,
(select tname from Teacher where tcode = Student.yujiao)as 语文老师 from Student join score on Student.scode =score.code
SQL连接查询的更多相关文章
- sql连接查询中的分类
sql连接查询中的分类 1.内连接(结果不保留表中未对应的数据) 1.1等值连接:关联条件的运算符是用等号来连接的. 1.2不等值连接:连接条件是出等号之外的操作符 1.3自然连接:特殊的等值连接,在 ...
- sql连接查询(inner join、full join、left join、 right join)
sql连接查询(inner join.full join.left join. right join) 一.内连接(inner join) 首先我这有两张表 1.顾客信息表customer 2.消费订 ...
- SQLServer SQL连接查询深度探险(摘录
SQL连接查询深度探险[摘录] 测试环境: Windows XP Profession MySQL 5.0.45 Oracle 9i DB2 UDB 9.1 测试的SQL脚本如下:此脚本适合MySQL ...
- SQL连接查询、变量、运算符、分支、循环语句
连接查询:通过连接运算符可以实现多个表查询.连接是关系数据库模型的主要特点,也是它区别于其它类型数据库管理系统的一个标志. 常用的两个链接运算符: 1.join on 2.union 在关系数据库 ...
- sql连接查询中on筛选与where筛选的区别
sql查询这个东西, 要说它简单, 可以很简单, 通常情况下只需使用增删查改配合编程语言的逻辑表达能力,就能实现所有功能. 但是增删查改并不能代表sql语句的所有, 完整的sql功能会另人望而生畏. ...
- MySQL学习(四) SQL连接查询
更多情况下,我们查询的数据来源于多张表,所有有必要了解一下MySQL中的连接查询. SQL中将连接查询分成四类:交叉连接,内连接,外连接和自然连接. 数据准备 student表 -- -------- ...
- sql 连接查询
什么是连接查询呢 概念:根据两个表或多个表的列之间的关系,从这些表中查询数据. 目的:实现多个表查询操作. 分类 首先划分一下,连接分为三种:内连接.外连接.交叉连接 内连接(INNER JOIN): ...
- SQL连接查询基础知识点
什么是连接 连接(join)查询是基于多个表中的关联字段将数据行拼接到一起,可以同时返回多个表中的数据. 下面以两个表为例子,举例说明一下不同的连接. SELECT * FROM products i ...
- SQL——连接查询
以mysql为例: 新建两张表table1和table2 CREATE TABLE `table1` ( `id` ) NOT NULL auto_increment, `name` ) defaul ...
随机推荐
- 一维Poisson方程计算
package com.smartmap.algorithm.equation.differential.partial.ellipsoidal; import java.io.FileOutputS ...
- SharePoint2013 错误
英文错误信息: This operation can be performed only on a computer that is joined to a server farm by users ...
- Understanding theory (1)
Source: verysmartbrothas.com It has been confusing since my first day as a PhD student about theory ...
- Android Studio常用快捷键
Ctrl+U :如果你的光标在重写父类的一个方法内(如:Activity#onCreate()),这个将会跳到父类的实现上. 如果你的光标在类名上,它将会跳到父类. Ctrl+Alt+Home:它可以 ...
- umeng 渠道统计 android
1.配置AndroidManifest.xml,添加权限 <uses-permission android:name="android.permission.ACCESS_NETWOR ...
- 【转】Android NFC学习笔记
一:NFC的tag分发系统 如果想让android设备感应到NFC标签,你要保证两点 1:屏幕没有锁住 2:NFC功能已经在设置中打开 当系统检测到一个NFC标签的时候,他会自动去寻找最合适的acti ...
- Xcode8 pod install 报错 “Generating Pods project Abort trap
Xcode8 pod install 报错 "Generating Pods project Abort trap 今天在写一个新项目的时候,使用cocoapods在执行 $ pod ins ...
- iOS-多线程--介绍NSOperration
一个NSOperation对象就代表一个操作,对象相当于GCD中的block. 一.NSOperation的作用: 配合使用NSOperation和NSOperationQueue也能实现多线程. 二 ...
- java文件复制函数
在写代码中,经常会需要复制文件.这时候就可以把这些函数放到工具类里面. 下面是一个复制文件的函数: public class CopyFileUtil { private static String ...
- 在网页中显示CHM (c# csharp .net asp.net winform)
CHM即“已编译的帮助文件”,主要由.hhc(目录文件)..hhk(索引文件)以及相应的帮助主题文件(.html,.htm)这些内容编译而成. 方法对比 在网页中显示CHM内容,大致有以下几种办法: ...