连接查询:同时设计两个及以上的表的查询

连接条件或连接谓词:用来连接两个表的条件一般格式:

[<表名1>]<列名1> <比较运算符> [<表名2>]<列名2>

[<表名1>]<列名1> between [<表名2>]<列名2> and [<表名2>]<列名3>

等值连接:

连接运算符为=

查询每个学生以及选修课程的情况

mysql> select student.*, sc.* from student,sc where student.sno=sc.sno;
+-------+--------+------+------+-------+-------+-----+-------+
| sno | sname | ssex | sage | sdept | sno | cno | grade |
+-------+--------+------+------+-------+-------+-----+-------+
| 95001 | 李勇 | 男 | 20 | CS | 95001 | 1 | 92 |
| 95001 | 李勇 | 男 | 20 | CS | 95001 | 2 | 85 |
| 95001 | 李勇 | 男 | 20 | CS | 95001 | 3 | 88 |
| 95002 | 刘晨 | 女 | 19 | IS | 95002 | 2 | 90 |
| 95002 | 刘晨 | 女 | 19 | IS | 95002 | 3 | 80 |
| 95004 | 张立 | 男 | 20 | IS | 95004 | 2 | 65 |
| 95004 | 张立 | 男 | 20 | IS | 95004 | 3 | NULL |
| 95004 | 张立 | 男 | 20 | IS | 95004 | 4 | NULL |
| 95005 | 张三 | 男 | 23 | CS | 95005 | 2 | 84 |
| 95005 | 张三 | 男 | 23 | CS | 95005 | 4 | NULL |
| 96001 | 刘军 | 男 | 30 | IS | 96001 | 1 | 87 |
| 96001 | 刘军 | 男 | 30 | IS | 96001 | 2 | 80 |
| 96001 | 刘军 | 男 | 30 | IS | 96001 | 3 | 90 |
| 96001 | 刘军 | 男 | 30 | IS | 96001 | 4 | 95 |
| 96001 | 刘军 | 男 | 30 | IS | 96001 | 5 | NULL |
| 96001 | 刘军 | 男 | 30 | IS | 96001 | 6 | NULL |
| 96001 | 刘军 | 男 | 30 | IS | 96001 | 7 | 86 |
| 97001 | 李四 | 男 | 26 | EN | 97001 | 4 | NULL |
| 97001 | 李四 | 男 | 26 | EN | 97001 | 5 | NULL |
+-------+--------+------+------+-------+-------+-----+-------+

自身连接:一个表与自己进行连接

需要给表起别名以示区别,且必须使用前缀

//查询每门课的间接先修课
mysql> select first.cno,second.cpno from course first,course second where first.cpno=second.cno;
+-----+------+
| cno | cpno |
+-----+------+
| | |
| | |
| | NULL |
| | NULL |
| | |
+-----+------+
rows in set (0.05 sec) //查询没门课先修课的名称
mysql> select FIRST.cno,FIRST.cname,FIRST.cpno,SECOND.cname from course FIRST,course SECOND where FIRST.cpno=SECOND.cno;
+-----+--------------+------+--------------+
| cno | cname | cpno | cname |
+-----+--------------+------+--------------+
| | 数据库 | | 数据结构 |
| | 信息系统 | | 数据库 |
| | 操作系统 | | 数据处理 |
| | 数据结构 | | C语言 |
| | C语言 | | 数据处理 |
+-----+--------------+------+--------------+

多表连接:两个以上的表进行连接

mysql> select student.sno,sname,cname,grade from student,sc,course where student.sno=sc.sno and sc.cno=course.cno;
+-------+--------+--------------+-------+
| sno | sname | cname | grade |
+-------+--------+--------------+-------+
| | 李勇 | 数据库 | |
| | 刘军 | 数据库 | |
| | 李勇 | 高等数学 | |
| | 刘晨 | 高等数学 | |
| | 张立 | 高等数学 | |
| | 张三 | 高等数学 | |
| | 刘军 | 高等数学 | |
| | 李勇 | 信息系统 | |
| | 刘晨 | 信息系统 | |
| | 张立 | 信息系统 | NULL |
| | 刘军 | 信息系统 | |
| | 张立 | 操作系统 | NULL |
| | 张三 | 操作系统 | NULL |
| | 刘军 | 操作系统 | |
| | 李四 | 操作系统 | NULL |
| | 刘军 | 数据结构 | NULL |
| | 李四 | 数据结构 | NULL |
| | 刘军 | 数据处理 | NULL |
| | 刘军 | C语言 | |
+-------+--------+--------------+-------+

排序:

select [ ] from <表名> order by <列名> [desc/asc];

mysql> select sno,sname from student order by sno; //默认为升序
+-------+--------+
| sno | sname |
+-------+--------+
| | bgg |
| | 山寨 |
| | 李勇 |
| | 刘晨 |
| | 王敏 |
| | 张立 |
| | 张三 |
| | 刘军 |
| | 芙蓉 |
| | 李四 |
+-------+--------+ mysql> select sno,sname from student order by sno asc; //asc 修饰为升序
+-------+--------+
| sno | sname |
+-------+--------+
| | bgg |
| | 山寨 |
| | 李勇 |
| | 刘晨 |
| | 王敏 |
| | 张立 |
| | 张三 |
| | 刘军 |
| | 芙蓉 |
| | 李四 |
+-------+--------+
rows in set (0.05 sec) mysql> select sno,sname from student order by sno desc; //desc 修饰为降序
+-------+--------+
| sno | sname |
+-------+--------+
| | 李四 |
| | 芙蓉 |
| | 刘军 |
| | 张三 |
| | 张立 |
| | 王敏 |
| | 刘晨 |
| | 李勇 |
| | 山寨 |
| | bgg |
+-------+--------+

mysql> select sno,sname from student order by sno desc,sname asc; //先按sno降序 再按sname升序
+-------+--------+
| sno   | sname  |
+-------+--------+
| 97001 | 李四    |
| 96004 | 芙蓉    |
| 96001 | 刘军    |
| 95005 | 张三    |
| 95004 | 张立    |
| 95003 | 王敏    |
| 95002 | 刘晨    |
| 95001 | 李勇    |
| 94001 | 山寨    |
| 12001 | bgg    |
+-------+--------+
10 rows in set (0.05 sec)

选择的between操作(在mysql/mariadb中是闭区间):

select * from <表名>  where <列名> between 'a' and 'b';

mysql> select * from student where sno between '' and '';
+-------+--------+------+------+-------+
| sno | sname | ssex | sage | sdept |
+-------+--------+------+------+-------+
| | 山寨 | 男 | | CS |
| | 李勇 | 男 | | CS |
| | 刘晨 | 女 | | IS |
| | 王敏 | 女 | | MA |
| | 张立 | 男 | | IS |
| | 张三 | 男 | | CS |
| | 刘军 | 男 | | IS |
+-------+--------+------+------+-------+ //not between and

mysql> select * from student where sno not between '94001' and '96001';
+-------+--------+------+------+-------+
| sno   | sname  | ssex | sage | sdept |
+-------+--------+------+------+-------+
| 12001 | bgg    | M    | 26   | CS    |
| 96004 | 芙蓉    | 女   | 32   | CH    |
| 97001 | 李四    | 男   | 26   | EN    |
+-------+--------+------+------+-------+

mysql模糊查询(LIKE)关键字:

% 替代一个或多个字符;

_ 仅替代一个字符;

[charlist] 字符列中的任何单一字符;

[!charlist] 或 [^charlist] 不在字符列中的任何单一字符;

//查询sno以95开头的学生信息
mysql> select * from student where sno like '95%';
+-------+--------+------+------+-------+
| sno | sname | ssex | sage | sdept |
+-------+--------+------+------+-------+
| | 李勇 | 男 | | CS |
| | 刘晨 | 女 | | IS |
| | 王敏 | 女 | | MA |
| | 张立 | 男 | | IS |
| | 张三 | 男 | | CS |
+-------+--------+------+------+-------+
rows in set (0.05 sec) //查询sno以01结尾的学生信息
mysql> select * from student where sno like '%01';
+-------+--------+------+------+-------+
| sno | sname | ssex | sage | sdept |
+-------+--------+------+------+-------+
| | bgg | M | | CS |
| | 山寨 | 男 | | CS |
| | 李勇 | 男 | | CS |
| | 刘军 | 男 | | IS |
| | 李四 | 男 | | EN |
+-------+--------+------+------+-------+
rows in set (0.05 sec)

//查询sno以01结尾的学生信息
//not like

mysql> select * from student where sno not like '%01';
+-------+--------+------+------+-------+
| sno   | sname  | ssex | sage | sdept |
+-------+--------+------+------+-------+
| 95002 | 刘晨    | 女   | 19   | IS    |
| 95003 | 王敏    | 女   | 19   | MA    |
| 95004 | 张立    | 男   | 20   | IS    |
| 95005 | 张三    | 男   | 23   | CS    |
| 96004 | 芙蓉    | 女   | 32   | CH    |
+-------+--------+------+------+-------+

IN操作符:

IN操作符允许在WHERE子句中规定多个值;

// 查询 sdept为MA,CS的信息
mysql> select * from student where sdept in ('MA','CS');
+-------+--------+------+------+-------+
| sno | sname | ssex | sage | sdept |
+-------+--------+------+------+-------+
| | bgg | M | | CS |
| | 山寨 | 男 | | CS |
| | 李勇 | 男 | | CS |
| | 王敏 | 女 | | MA |
| | 张三 | 男 | | CS |
+-------+--------+------+------+-------+

2018-05-01  20:24:33

mysql/mariadb学习记录——查询的更多相关文章

  1. mysql/mariadb学习记录——查询2

    Alias——使用一个列名别名AS 关键字: mysql> select sno as studentId,sname as studentName from student; +------- ...

  2. mysql/mariadb学习记录——查询3(AVG、SUM、COUNT)函数

    AVG() 求平均数函数: //求emp表中的sal属性的平均值 mysql> select avg(sal) as salAverage from emp; +-------------+ | ...

  3. mysql/mariadb学习记录——连接查询(JOIN)

    //本文使用的数据表格//persons表中id_p为主键//orders表中id_o为主键,id_p为外键参考persons表中的id_p mysql> select * from perso ...

  4. mysql/mariadb学习记录——limit

    在mysql/mariadb 中可以用limit来限制查询的条数.例子如下: 1.limit后加一个参数 limit n: //选中查询所有结果中的前两条记录并返回 mysql> ; +---- ...

  5. mysql/mariadb学习记录——创建删除数据库、表的基本命令

    查看已有的数据库: mysql> show databases; +--------------------+ | Database | +--------------------+ | inf ...

  6. MySQL/MariaDB数据库的查询缓存优化

    MySQL/MariaDB数据库的查询缓存优化 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL架构 Connectors(MySQL对外提供的交互接口,API): ...

  7. MySQL/MariaDB学习笔记——mysql.user表中存在多个root用户问题理解

    mysql.user表中存在多个root用户问题 问题描述:使用 SELECT host,user FROM mysql.user 发现mysql.user表中存在三个root用户,如下 持着对中几个 ...

  8. MariaDB学习记录

    MariaDB的学习 MariaDB的学习 关于MariaDB的历史,不再概述 下面是mariadb的官网:https://mariadb.com/ 同样的,MariaDB一样有连接java的jar包 ...

  9. mysql的学习记录

    1 MySQL -h localhost -u UserName -p Password-h不写,默认为localhost注意:最好先MySQL -h localhost -u UserName -p ...

随机推荐

  1. 如何将钉钉集成到FineReport插件中

    报表服务器 安装钉钉管理插件后,打开报表管理平台,管理系统下会增加钉钉管理节点,钉钉相关的配置管理都将会放在这个节点中去配置: 同时,设置定时任务的最后一步输出设置中,会增加推送钉钉消息: 钉钉企业应 ...

  2. 生成项目目录结构(based on windows system)

    描述: 作为程序员,在工作中,我们经常会有需求,需要罗列出项目的结构图:如果手工来整理的话,太过浪费时间,其实我们可以借助tree命令来快速生成目录结构. 本文主要介绍一下,基于windows系统,如 ...

  3. 数组转换list集合问题

    1问题的起因: 项目总要使用一个方法把数组转换成list集合,百度说有这个方法Arrays.asList可以实现 2结果报错 抛出java.lang.UnsupportedOperationExcep ...

  4. Centos 使用C++11 编译

    今天编译代码,发现使用auto后无法编译,我的当前linux内核版本:(4.7之后即可支持C++11) 这时,在编译末尾加入 -std=c++11 就可以正常编译了.如:

  5. git pull 错误:The following untracked working tree files would be overwritten by merge

    错误描述: $ git pull origin alphaFrom https://github.com/shirley-wu/HeartTrace * branch            alpha ...

  6. 记开发个人图书收藏清单小程序开发(三)DB设计

    主要是参考豆瓣的图书查询接口: https://api.douban.com/v2/book/isbn/:9780132350884 返回内容如下: { "rating": { & ...

  7. uml各类图

    原文:http://www.cnblogs.com/way-peng/archive/2012/06/11/2544932.html 一.UML是什么?UML有什么用? 二.UML的历史 三.UML的 ...

  8. 解决UITableView在iOS7中UINavigationController里的顶部留白问题

    解决UITableView在iOS7中UINavigationController里的顶部留白问题 出现问题时候的截图: 源码: 用到的类: UIViewController+TitleTextAtt ...

  9. Linux chkconfig命令详解

    chkconfig命令检查.设置系统的各种服务.这是Red Hat公司遵循GPL规则所开发的程序,它可查询操作系统在每一个执行等级中会执行哪些系统服务,其中包括各类常驻服务.谨记chkconfig不是 ...

  10. oracle 28001错误 密码过期失效

    背景 服务器演示地址,无法使用.排查原因,是数据库密码有问题,报28001错误 问题 确定是oracle密码机制,180失效了 解决 1.进入sqlplus模式 sqlplus / as sysdba ...