一、外连接

1.左连接  left join 或 left outer join

SQL语句:select * from student left join score on student.Num=score.Stu_id;

2.右连接  right join 或 right outer join

SQL语句:select * from student right join score on student.Num=score.Stu_id;

3.完全外连接  full join 或 full outer join

SQL语句:select * from student full join score on student.Num=score.Stu_id;

通过上面这三种方法就可以把不同的表连接到一起,变成一张大表,之后的查询操作就简单一些了。

而对于select * from student,score;则尽量不使用此语句,产生的结果过于繁琐。

二、内连接 

join 或 inner join

SQL语句:select * from student inner join score on student.Num=score.Stu_id;

此时的语句就相当于:select * from student,score where student.ID=course.ID;

三、交叉连接

cross join,没有where指定查询条件的子句的交叉联接将产生两表的笛卡尔积。

SQL语句:select * from student cross join score;

四、结构不同的表连接

当两表为多对多关系的时候,我们需要建立一个中间表student_score,中间表至少要有两表的主键。

SQL语句:select s.Name,C.Cname from student_score as sc left join student as s on s.Sno=sc.Sno left join score as c on c.Cno=sc.Cno

select C_name,grade from student left join score on student.Num=score.Stu_id where name='李五一';

红色部分即中间表,是集合两表所有内容的一张总表。

五、UNION操作符用于合并两个或多个select语句的结果集。

UNION内部的SELECT语句必须拥有相同数量的列,每个列也必须拥有相似的数据类型,每条SELECT语句中的列的顺序必须相同。

select Num from student union select Stu_id from score;

union操作符是默认查重的,如果允许重复的值,就可以使用union all 。对于两张结构相同的表,union也可以把他们合并成一张表:

select * from student1 union select *from student2;

六、子查询

有时候,查询时需要的条件是另外一个select语句的结果,就会使用到子查询。

1.带IN关键字的子查询

SQL语句:select * from student where Num IN(select Stu_id from score);

2.带EXISTS关键字的子查询

exists内查询返回一个真价值,若返回true时,外查询进行查询,否则外查询不进行查询。

SQL语句:select * from student where exists(select * from score where C_name='计算机');

3.带ANY关键字的子查询

使用ANY关键字只要有一个满足,就通过该条件来执行外查询。

SQL语句:select sname,(date_format(from_days(now())-to_days(birthday)),'%Y')+0) as '年龄' from student where birthday>ANY(select birthday from student where bumen='计算机系');

4.带ALL关键字的子查询

使用ALL关键字必须满足所有的内层查询语句返回的所有结果,才执行外查询

SQL语句:select sname,(date_format(from_days(now())-to_days(birthday)),'%Y')+0) as '年龄' from student where birthday>ALL(select birthday from student where bumen='计算机系');

暂时学了这么多内容,以后有更好的方式再来补充。

SQL语句多表连接查询语法的更多相关文章

  1. SQL的多表连接查询

    SQL的多表连接查询 多表连接查询具有两种规范,SQL92和SQL99规范. SQL92规范支持下列多表连接查询: (1)等值连接: (2)非等值连接: (3)外连接: (4)广义笛卡尔积: SQL9 ...

  2. MySQL常用sql语句-----数据表的查询操作

    常用的sql语句如下,应对工作足以 1.查询指定字段 select c_id,c_age,c_name from t_student; select c_id as 编号,c_name as 姓名,c ...

  3. sql语句左右表连接理解

    一句话,左连接where只影响坐标,右连接where只影响右表

  4. sql语句 两表关联查询计算数量

    select sum(a1.`num`)   from `order_orderlistrow` as a1 INNER JOIN `order_orderlist` as a2 on a1.`ord ...

  5. 5-04用Sql语句创建表

    用Sql语句创建表的基本语法: USE E_Market--指向当前所操作的数据库 GO CREATE TABLE CommoditySort--创建表的名字 { sortID int IDENTIT ...

  6. oracle(sql)基础篇系列(二)——多表连接查询、子查询、视图

        多表连接查询 内连接(inner join) 目的:将多张表中能通过链接谓词或者链接运算符连接起来的数据查询出来. 等值连接(join...on(...=...)) --选出雇员的名字和雇员所 ...

  7. oracle(sql)基础篇系列(二)——多表连接查询、子查询、视图

    多表连接查询 内连接(inner join) 目的:将多张表中能通过链接谓词或者链接运算符连接起来的数据查询出来. 等值连接(join...on(...=...)) --选出雇员的名字和雇员所在的部门 ...

  8. 010.简单查询、分组统计查询、多表连接查询(sql实例)

    -------------------------------------day3------------ --添加多行数据:------INSERT [INTO] 表名 [(列的列表)] --SEL ...

  9. SQL多表连接查询(详细实例)

    转载博客:joeleo博客(http://www.xker.com/page/e2012/0708/117368.html) 本文主要列举两张和三张表来讲述多表连接查询. 新建两张表: 表1:stud ...

随机推荐

  1. PHP——基本使用(一)

    Apache安装与配置 install 下载地址:https://www.apachelounge.com/download/,选择2.4.33版本64位 将程序解压到一个英文目录下,以管理身份打开c ...

  2. 并发编程学习笔记(3)----synchronized关键字以及单例模式与线程安全问题

    再说synchronized关键字之前,我们首先先小小的了解一个概念-内置锁. 什么是内置锁? 在java中,每个java对象都可以用作synchronized关键字的锁,这些锁就被称为内置锁,每个对 ...

  3. Install Zabbix with Docker

    1. mysql -uroot -p -h10.10.0.242 zabbix<schema.sqlEnter password: * ERROR 1709 (HY000) at line 86 ...

  4. 微服务网关从零搭建——(三)Ocelot网关 + identity4

    增加验证服务 1.创建名为AuthService 的core 空项目 2.修改startup文件 using System; using System.Collections.Generic; usi ...

  5. script标签属性sync和defer

    <script src="a.js" defer></script> 加了defer属性script标签的页面,运行流程如下:   1.浏览器开始解析HTM ...

  6. MAC 打开Chrome打开开发者工具的快捷键

    mac下safari和chrome打开开发者工具的快捷键相同,都是 option(alt)+command+i 这个是我的默认配置,没有更改过的.

  7. 散列--P1047 校门外的树

    题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0,1,2,-,L,都种有 ...

  8. UVA-1589 象棋(模拟)

    题目:(传送门) 给出一个象棋的残局,下一步是黑棋走,判断黑棋是不是被将死. 思路: 读完这个题,知道是一个模拟题,然后想到用两个二维数组来模拟棋盘,一个(mp数组)用来存残局,一个(res数组)用来 ...

  9. 山建校赛B题公式证明

    原题 证明

  10. (二)Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用

    #!usr/bin/env python # -*- coding: utf-8 -*- def test(): print('hello, world') if __name__ == " ...