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

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

[<表名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. 【node】用koa搭建一个增删改服务(一)

    前文,vue分类里有一个日志demo的练习,这篇文章就是介绍针对日志demo的服务是怎么写的 一.koa搭建项目 1. npm init 2. npm install koa 二.建数据库 下面是项目 ...

  2. CSS的设计模式

    什么是设计模式? 曾有人调侃,设计模式是工程师用于跟别人显摆的,显得高大上:也曾有人这么说,不是设计模式没用,是你还没有到能懂它,会用它的时候. 先来看一下比较官方的解释:“设计模式(Design p ...

  3. web 学习资源

    学习 https://skills.bugbank.cn/ https://github.com/JnuSimba/MiscSecNotes 靶场 http://skysec.top/2018/01/ ...

  4. Pig parallel reduce并行执行数

    parallel语句可以附加到Pig Latin中任一个关系操作符后面,然后它会控制reduce阶段的并行,因此只有对与可以触发reduce过程的操作符才有意义.     可以触发reduce过程的操 ...

  5. h5调用微信分享

    https://blog.csdn.net/qq_39562787/article/details/79217386

  6. 如何修改ionic中android程序的包名

    默认ionic新建工程的时候指定的Android版本包名是:com.ionicframework.starter:这样固定死包名的话会导致一个问题,多个ionic工程无法正常安装到手机当中,后面安装的 ...

  7. “拒绝了对对象数据库的 EXECUTE 权限”之解决

    “拒绝了对对象'aspnet_CheckSchemaVersion'的 EXECUTE 权限”之解决 [错误状态] “/XXX”应用程序中的服务器错误. ----------------------- ...

  8. mysqlcilent的安装

    这软件包是贼的气 首先是windows的安装上你必须要指定版本,linux的安装你就不需要指定版本了 windos上的指定安装必须要   一.下载包的时候需要指定版本, 比如python2的和mysq ...

  9. flask的orm操作

    django是有orm操作的  可想而知 那么flask也是有orm操作的,其实flask的orm操作的使用和djnago的是差不多的 django的orm操作进行条件筛选的时候后面跟着的是objec ...

  10. consul日常操作命令

    #开发模式运行agent consul agent -dev #查看consul 集群成员 consul members [-detailed] members命令的输出基于gossip协议,并最终一 ...