废话不多说,直接进入正题

#数据准备

班级表class:

CREATE TABLE `class` (
`class_no` int(2) unsigned zerofill NOT NULL AUTO_INCREMENT COMMENT '班级编号',
`class_name` varchar(30) CHARACTER SET utf8 NOT NULL COMMENT '班级名称',
PRIMARY KEY (`class_no`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
insert into class values(1, '培优班');
insert into class values(2, '普通班');
insert into class values(3, '提升班');

学生表student:

CREATE TABLE `student` (
`stu_no` int(2) unsigned zerofill NOT NULL AUTO_INCREMENT COMMENT '学员编号',
`stu_name` varchar(30) CHARACTER SET utf8 NOT NULL COMMENT '学员姓名',
`stu_sex` varchar(3) CHARACTER SET utf8 NOT NULL COMMENT '学员性别',
`stu_age` tinyint(2) unsigned zerofill DEFAULT NULL COMMENT '学员年代',
`grade` double(5,2) unsigned zerofill DEFAULT NULL COMMENT '成绩',
`class_no` int(2) unsigned zerofill DEFAULT NULL COMMENT '所在班级编号',
PRIMARY KEY (`stu_no`),
KEY `class_no` (`class_no`)
) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=latin1; insert into student values(01, '李白', '男', 18, 60, 01);
insert into student values(02, '杜甫', '男', 20, 76, 01);
insert into student values(03, '张飞', '男', 32, 80, 02);
insert into student values(04, '韩信', '男', 26, 98, 02);
insert into student values(05, '了龙', '男', 27, 56, 02);
insert into student values(06, '大乔', '女', 17, 88, 01);
insert into student values(07, '小乔', '女', 16, 96, 01);
insert into student values(08, '小乔', '女', 16, 90, 01);
insert into student values(09, '关哥', '男', 32, 80, 02);
insert into student values(10, '刘备', '男', 36, 98, null);

1: exists子查询

如果子查询有返回结果则为true,如果没有返回值则为false

select * from student where exists(select * from student where grade = 80)

比如not exists:

select * from student where not exists(select * from student where grade = 80);

以上结果返回空,因为 not exists 返回了 false

select * from student where exists (select * from class where class.class_no = student.class_no);

上面的查询可以看到,我们少了一条数据,第十条的clas_no 是null。。。所以这条数据是flase....而

class.class_no = student.class_no 为true的,就全部返回了

 2: [union] 并合查询

需求: 拿到01班级的最高成绩 和 02班级的最低成绩

我们一般会这样

select max(grade) from student where class_no = 01;
select min(grade) from student where class_no = 02;

优化这个查询我们可以这样:

(select concat('1号班级最高成绩:', max(grade)) '成绩' from student where class_no = 01)
union
(select concat('2号班级最低成绩:', min(grade)) '成绩' from student where class_no = 02);

这里再说下union 和union all的区别:

union:

(select class_no, stu_name, stu_age from student where class_no = 1)
union
(select class_no, stu_name, stu_age from student where class_no = 2);

union all:

(select class_no, stu_name, stu_age from student where class_no = 1)
union all
(select class_no, stu_name, stu_age from student where class_no = 2);

通过以上两个查询,我们可以看到:union并合查询它会自动的去重复的记录, 如果不想要去掉重复的记录则可以使用 union all;

我们加个排序:

(select class_no, stu_name, stu_age from student where class_no = 1)
union all
(select class_no, stu_name, stu_age from student where class_no = 2) order by stu_age desc;

连接查询的分类
    1: 内连接
    2: 外连接
    3: 自然连接

1: inner join(内连接)
    需求: 查询出学员的学号, 姓名, 所在的班级名称

select stu_no, stu_name, class_name from student  join class where `student`.class_no = `class`.class_no;

以上sql等同于:

select stu_no, stu_name, class_name from student join class where `student`.class_no = `class`.class_no;
select stu_no,stu_name,class_name from student,class where student.class_no = class.class_no;

内连接的inner字符可以不用写

2: cross join(交叉连接,迪卡尔集) 没有条件的内连接
    例: select * from student cross join class;
    例: select * from student inner join class;
    例: select * from student cross join class where `student`.class_no = `class`.class_no;
    ps: cross join 与 inner join 在使用上没有区分,只是在mysql中把cross join定义成交叉连接而已

就写到这把。。其他的连接方式也简单,资料也很多啦。。。

mysql 关联查询技巧的更多相关文章

  1. MySQL关联查询总结

    MySQL中经常使用关联查询,有机会总结下: 1 left join(左联查询): 返回包括左表中的所有记录和右表中联接字段相等的记录 例:select * from a left join b on ...

  2. mysql 关联查询 索引不起作用原因记录

    业务逻辑如下:查询某篇文章的评论列表,且列出评论人及被评论人的昵称.头像. 先看一下表结构 评论表: 评论表的索引: 用户表: 用户表的索引: 查询语句如下: SELECT t1.comment_id ...

  3. mysql关联查询和联合查询

    一.内联方式 1.传统关联查询 "select * from students,transcript where students.sid=transcript.sid and transc ...

  4. MySQL☞关联查询

    关联查询:所需要的数据来源于多张表,通过表的连接查询(关联查询)来查询多张表中的数据 格式: select 别名1 . */列名 , 别名2 . */列名 from 表名1  别名1 , 表名2  别 ...

  5. [MySQL]多表关联查询技巧

    示例表A: author_id author_name 1 Kimmy 2 Abel 3 Bill 4 Berton 示例表B: book_id author_id start_date end_da ...

  6. [mysql] 关联查询sql记录

    //查询账单关联订单 select o.id as id, o.order_no as orderNo, o.case_no as caseNo, o.send_time as sendTime, o ...

  7. MySQL 关联查询 内连接

    内连接    [INNER| CROSS] JOIN无条件内连接:无条件内连接,又名交叉连接/笛卡尔连接第一张表种的每一项会和另一张表的每一项依次组合#例:mysql>  select  *  ...

  8. mysql关联查询

    mysql数据库的统计------生成统计信息 1.distinct:在一组之中将各个唯一的值找出来,如找出所有的品牌种类 mysql>select distinct brand_kind fr ...

  9. mysql 关联查询的执行顺序

    STRAIGHT JOIN : 能强制按照顺序关联表(应该是)

随机推荐

  1. LAMP安装教程

    LAMP环境配置安装注意安装步骤及说明事项. Linux + apache+mysql+php 附件: 1. 访问ftp报错 解决: 关闭selinux vi  /etc/selinux/config ...

  2. MySQL下创建数据库以及授权用户

    一.新建数据库 1.首先登录MySQL:(输入 mysql -u root -p 命令,然后输入密码按回车即可) 2.在mysql> 下输入如下命令,回车,即可创建数据库 (test为数据库名) ...

  3. unigui ShowModal、MessageDlg

    procedure Calback(Sender: TComponent; AResult: Integer);procedure TForm.Calback(Sender: TComponent; ...

  4. 剑指C++面试

    传闻公司老总欠下巨款,带着小姨子跑路了~  树倒猢狲散,接下来要好好准备面试,以期找到一份满意的工作. 面试准备分下面几个方面进行,形成面试系列文章,文章内容以问答的方式呈现. 1.C++语言基础 传 ...

  5. 背水一战 Windows 10 (122) - 其它: 通过 Windows.System.Profile 命名空间下的类获取信息, 查找指定类或接口的所在程序集的所有子类和子接口

    [源码下载] 背水一战 Windows 10 (122) - 其它: 通过 Windows.System.Profile 命名空间下的类获取信息, 查找指定类或接口的所在程序集的所有子类和子接口 作者 ...

  6. 利用Swashbuckle生成Web API Help Pages

    利用Swashbuckle生成Web API Help Pages 本文将通过Swagger的.NET Core的实现封装工具Swashbuckle来生成上一篇-<创建ASP.NET Core ...

  7. 应用篇 = Docker下的Redis

    一.工具介绍 1.1 什么是Docker? Docker 最初是 dotCloud 公司创始人 Solomon Hykes 在法国期间发起的一个公司内部项目.使用 Google 公司推出的 Go 语言 ...

  8. Java初学者容易犯的代码错误

    1. 不会判断空 空指针异常是所有Java初学者接触最多的异常,没有之一.原因是,你们拿到一个对象后容易不假思索的直接使用(直接给这个对象的属性赋值,直接调用这个对象的方法等),不报异常才怪呢!下面是 ...

  9. Javascript高级编程学习笔记(64)—— 事件(8)键盘与文本事件

    键盘与文本事件 用户在使用键盘时会触发键盘事件 “DOM2级事件”最初规定了键盘事件,但是最后在定稿时又删除了相应内容 所以键盘事件被放入了DOM3级事件的规范中 总的来说有三个键盘事件: keydo ...

  10. python 中numpy dot函数的使用方法

    这个函数在的数字信号处理中用处还是比较广泛的,函数的具体定义如下所示: numpy.dot(a, b, out=None) 该函数的作用是获取两个元素a,b的乘积,表示的含义如下所示: dot(a, ...