mysql 连接查询索引_Mysql (四)连接查询和索引
一、什么是连接查询:就是将二个或二个以上的表,“连接起来”当做一个数据源,并从中去取得所须要的数据。连接查询包括交叉连接查询、内连接查询、外连接查询
(一)交叉连接:交叉连接不带WHERE子句,它返回被连接的两个表所有数据行的笛卡尔积,返回结果集合中的数据行数等于第一个表中符合查询条件的数据行数乘以第二个表中符合查询条件的数据行数。
1、格式1:select 要查询的内容 from 表名1 cross join 表名2;
2、格式2:select 要查询的内容 from 表名1,表名2…;
3、格式3:select 要查询的内容 from 表名1 join 表名2;
select * from department,employee;
(二)内连接:内连接也叫连接,是最早的一种连接。还可以被称为普通连接或者自然连接,内连接是从结果表中删除与其他被连接表中没有匹配行的所有行,所以内连接可能会丢失信息。
1、方法1:select 要查询的内容 from 表名1,表名2… where 表1.字段名=表2.字段名;
2、方法2:select 要查询的内容 from 表名1 join 表名2 where 表1.字段名=表2.字段名;
3、方法3:select 要查询的内容 from 表名1 cross join 表名2 where 表1.字段名=表2.字段名;
4、方法4:select 要查询的内容 from 表名1 inner join 表名2 on 表1.字段名=表2.字段名;
5、方法5:select 要查询的内容 from 表名1 join 表名2 on 表1.字段名=表2.字段名;
6、注意:在使用内连接时,可以给表起别名,格式为select 要查询的内容 from 表名1 别名1 join 表名2 别名2 on 别名1.字段名=别名2.字段名;
select * from department inner join employee on department.did = employee.did;
select * from department as d inner join employee as e on d.did = e.did;
select * from department d inner join employee e on d.did = e.did;
select * from department d inner join employee e on d.did > e.did;
select department.*,employee.id,name,age from department inner join employee on department.did = employee.did;
自然连接
select * from department natural join employee;
select * from orders cross join customers;
select * from orders inner join customers on customers.customer_id = orders.customer_id ;
(三)外链接:只限制一张表中的数据必须满足连接条件,而另一张表中的数据可以不满足连接条件的连接方式,外连接分为左外连接、右外连接、全外连接
1、左外连接(以左表为主,即左表中的内容会全部显示出来,右表中不符合条件的部分用null补充):select 要查询的内容 from 表1 left join 表2 on 表1.字段名=表2.字段名;
2、右外连接(以右表为主,即右表中的内容会全部显示出来,左表中不符合条件的部分用null补充)):select 要查询的内容 from 表1 right join 表2 on 表1.字段名=表2.字段名;
select * from orders left outer join customers on orders.customer_id = customers.customer_id;
select * from orders right outer join customers on orders.customer_id = customers.customer_id;
orders表的数据都保留下来,左表数据完全显示,右边一一对应,没能连上null
二 union关键字的使用——联合查询:联合查询用于查询同一张表,但是需求不同
1) 作用:UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中。多个 SELECT 语句会删除重复的数据。
2) 格式:查询语句1 union select 查询语句2;
3) 注意事项: union 内部的 select语句必须拥有相同数量的列 列拥有相似的数据类型
每条 select语句中要显示的列的顺序相同
union返回结果的标题集仅从第一个查询中获得,无论第二个查询如何命名或取别名都不会更改
在联合查询中: order by不能直接使用,需要对查询语句使用括号才行;另外,要orderby生效: 必须搭配limit: limit使用限定的最大数即可,示例: 如查询学生成绩, 男生成绩降序升序, 女生成绩升序。 (select * from student inner join score on student.sno = score.sno where ssex = '女' order by degree limit 9999) union (select * from student inner join score on student.s no = score.sno where ssex = '男' order by degree desc limit 9999);
select * from orders union select *from orders;
select * from orders union all select *from orders;
select * from student inner join score on student.sno = score.sno where ssex = '男' order by degree union select * from student inner join score on student.sno = score.sno where ssex = '女' order by desc;
对于u和ob使用错误
(select * from student inner join score on student.sno = score.sno where ssex = '男' order by degree) union (select * from student inner join score on student.sno = score.sno where ssex = '女' order by desc);
(select * from student inner join score on student.sno = score.sno where ssex = '男' order by degree limit 9999) union (select * from student inner join score on student.sno = score.sno where ssex = '女' order by desc limit 9999);
括号,limit条目数
select * from a inner join b on a.category_id =b.category_id;
select category_id from a where name = '科技';
select title from b where category_id =1;
select title from b where category_id = (select category_id from a where name = '科技'); 返回一个值
select category_id from a; 返回一列
三 子查询之exists关键字
drop table if exists a,b; 有就删,没也不报错
select * from a where score = 100;
select * from a where exists (select * from a where score = 100);
select * from a where exists (select name from a where score = 100);
select * from a where exists (select id from a where score = 100);
子查询有返回值,执行
select * from a where exists (select 200);
select * from a where exists (select null);
四 distinct关键字 去重
select name from a where score < 60;
select name from a where name not in (select name from a where score <60);
select distinct name from a where name not in (select name from a where score <60);
select * from a where age =(select max(age) from a) and height = (select max(height) from a);
select * from score where degree >80;
select sno,max(degree) from (select * from score where degree > 80) group by sno; 表子查询要起别名
select sno,max(degree) from (select * from score where degree > 80)as a group by sno;
Every derived table must have its own alias 每个派生表
必须有自己的别名
五 子查询之any、all关键字的使用
select * from tab2 where id >any (select f1 from tab3); 2的id>any
select * from tab2 where id >all (select f1 from tab3);
select * from tab2 where id =any (select f1 from tab3);
select * from tab2 where id >=any (select f1 from tab3);
select * from tab2 where id >all (select f1 from tab3);
select * from tab2 where id
一、 索引:索引在MySQL中也叫做“键”,是存储引擎用于快速找到记录的一种数据结构。索引对于良好的性能非常关键,尤其是当表中的数据量越来越大时,索引对于性能的影响愈发重要。索引相当于字典的音序表,如果要查某个字,如果不使用音序表,则需要从几百页中逐页去查。索引通常给where后一、 索引的优缺点
(一) 优点:为了加快搜索速度,减少查询时间
(二) 缺点:
1、 索引是以文件存储的。如果索引过多,占磁盘空间较大。而且他影响: insert ,update ,delete 执行时间。
2、 索引中数据必须与数据表数据同步:如果索引过多,当表中数据更新的时候后,索引也要同步更新,这就降低了效率。
二、 索引的分类:普通索引、唯一索引、单列索引、多列索引等
(一) 普通索引:不应用任何限制条件的索引,可以在任何数据类型中创建
(二) 唯一索引:与普通索引类似,不同的就是:索引列的值必须唯一,但允许有空值,即使有多个空值也是可以的(注意和主键不同,主键是一种特殊的唯一索引)。如果是组合索引,则列值的组合必须唯一,创建方法和普通索引类似。常用于电话号码、身份证等
(三) 单列索引:只对应一个字段的索引
(四) 多列索引:多个字段上创建一个索引
三、 索引的创建方式
(一) 建表时创建索引
create table 表名(
字段名1 数据类型 约束,
字段名2 数据类型 约束,
….
索引类型 index 索引名(字段名 排序方式)/索引类型 key 索引名(字段名)
);
注意:
1. 采用该方式创建索引时,索引名称,可以加也可以不加,不加使用字段名作为索引名。
2. 索引类型也可以省略:unique、fulltext、spatial
3. 排序方式:asc、desc
create unique index on a(id asc); 不可
create unique index my_index on a(id asc);
create 索引类型 index/key 索引名 on 表名(字段名 排序方式) 中间无逗号
create index name on a (name(10) desc);
create unique index age on a(age asc);
alter table a add 字段名 数据类型 约束;
alter table a change 旧字段 新字段 数据类型 约束;
alter table a drop 字段名;
alter table a add 索引类型 index/key 索引名 (字段名(长度) 排序方式);
alter table a add index gender(gender(2) asc);
四、 查看索引
show create table a;
show index from a;
show keys from a;
五、 删除索引
drop index my_index on a; key name
alter table a drop index name;
六、 主键索引的添加
(一) 建表时添加格式1:
create table 表名(
字段名1 数据类型 primary key,
字段名2 数据类型 约束,
….
索引类型 index 索引名(字段名 排序方式)/索引类型 key 索引名(字段名)
);
(二) 建表时添加格式2:
create table 表名(
字段名1 数据类型 primary key,
字段名2 数据类型 约束,
….
primary key(字段名)
);
create table a(
id int,
name char(10),
gender char(2),
address varchar(100),
age int,
index (name),
index(id),
index(age)
)engine = myisam; 普通索引,单列索引
create table a(
id int,
name char(10),
unique index(id)
)engine = myisam;
insert into a values(1,'a');
insert into a values(2,'b');
insert into a values(2,'c'); 唯一
insert into a (name) values ('c'); 非空
create table a(
ip char(20),
port int,
access char(10),
unique index(ip,port)
);
(三) 表建成后添加:alter table 表名 add primary key(字段名);
注意:主键索引也可以被设置为复合主键索引,就是由多个字段组合形成主键,示例如下(用ip和port组合形成主键)
alter table a add primary key(id);
alter table a change id id int auto_increment; 加自增。有主键才能加
七、 主键索引的删除:alter table 表名 drop primary key;注意:如果主键上有auto_increment,那么必须先将auto_increment删除才能删除主键索引
alter table a drop primary key; 有报错
alter table a change id id int ; 删自增
alter table a add primary key(id); 加主键
alter table a drop primary key; 先删自增,才能删主键
mysql 连接查询索引_Mysql (四)连接查询和索引相关推荐
- mysql多表连接 索引_MySQL多表查询之外键、表连接、子查询、索引
一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复的,不允许为空,用来保证数据完整性 外键:是另一表的主键, ...
- 小白 MySQL数据库链接查询语句_MySQL数据库——连接查询
今天将用 "手" 来教大家关于MySQL连接查询的知识! ============================================================= ...
- mysql跨库查询 索引_MySQL中跨库查询怎么搞?
导读 在MySQL中跨库查询主要分为两种情况,一种是同服务的跨库查询;另一种是不同服务的跨库查询;它们进行跨库查询是不同的,下面就具体介绍这两种跨库查询. 在MySQL中跨库查询主要分为两种情况,一种 ...
- mysql 回表查询优化_mysql:若何行使笼罩索引制止回表优化查询
说到笼罩索引之前,先要领会它的数据结构:B+树. 先建个表演示(为了简朴,id按顺序建): id name 1 aa 3 kl 5 op 8 aa 10 kk 11 kl 14 jk 16 ml 17 ...
- mysql的基本的查询语句_Mysql的基本查询语句
聚集函数 mysql有5个聚集函数,分别是AVG,MAX,MIN,SUM,COUNT. 分组 分组的使用group by作为关键字,having作为条件关键字. having和where的区别:1.w ...
- mysql 单表子查询语句_MySQL基本SQL查询语句:多表查询和子查询示例
一.简单查询:基本语法: 代码如下 SELECT * FROM tb_name; 查询全部 代码如下 SELECT field1,field2 FROM tb_name; 投影 代码如下 SELECT ...
- mysql多表查询书籍_MySQL多表查询及子查询
1. MySQL数据库执行查询操作时的查询流程: 请求-->查询缓存 请求-->查询缓存-->解析器-->预处理器-->优化器-->查询执行引擎-->存储引擎 ...
- mysql 短时大连接的问题_mysql长连接和短连接的问题
什么是长连接? 其实长连接是相对于通常的短连接而说的,也就是长时间保持客户端与服务端的连接状态. 通常的短连接操作步骤是: 连接->数据传输->关闭连接: 而长连接通常就是: 连接-> ...
- mysql多表联合查询事例_MySQL——多表查询详细介绍以及实例
1.表与表之间的关系 一对一:用户表和身份信息表,用户表是主表 例如:男人表 .女人表create table man( mid int primary key auto_increment, mna ...
最新文章
- eclipse调试详解
- python3入门与进阶-python3入门与进阶(一)
- UVA 11178 Morley’s Theorem(莫雷定理 计算几何)
- 测试Hibernate的最低配置
- 【剑指offer】【leetcode精选题集】【Java】剑指offer题解合集 更新中
- android 模糊读取文件名_Android 从路径中获取文件名 | 学步园
- wps怎么画网络图_尼玛,WPS竟然悄悄搞了个免费的公文写作AI...【石说】
- python csv修改文件_Python实例:对CSV文件的操作
- ubuntu14.04下svn版本管理系统的安装及常用命令的使用整理
- MplusAutomation包的使用 二
- ffmpeg 音乐循环_Android使用FFmpeg(四)--ffmpeg实现音频播放(使用AudioTrack进行播放)
- 面试中常被问到(七)封装继承多态知多少
- ios-AddressBook框架
- 新买电脑如何做到长时间不卡顿
- 要么出众,要么出局 定制化让企业更出众
- 9个Excel小技巧,提高你的数据分析效率
- Topic 17. 临床预测模型之缺失值识别及可视化
- (wps)表格下拉选择多个选项
- 什么是CRM?CRM定义/CRM适用于谁?/CRM系统有什么作用?
- 2021宝德数字产业生态大会 | 计算驱动未来
热门文章
- 深度评测 Acer 掠夺者刀锋500SE 2021 怎么样
- 【老生谈算法】matlab实现图像滤波处理算法源码——图像滤波处理算法
- 计算机考试准考证号忘了如何查询成绩
- 本题要求实现求Fabonacci数列项的函数。所谓Fibonacci数列就是满足任一项数字是前两项的和(最开始两项均定义为1)的数列。
- 虚拟机安装mac无法在更新服务器失败,解决VMware虚拟机安装 Mac os,安装VMware tools不成功或无法全屏的问题...
- Intellij IDEA官方最完美编程字体Mono
- mysql 修改表空间名称_修改多个表空间的数据文件的名称和位置
- 关于AIRIA翻译的FLEX教学质量我个人的看法
- Java学习路线(进阶篇二)
- 李国庆做客王峰十问:为何大佬们突然呼唤996?