MYSQL多表查询

1、 一层嵌套子查询 --查询出来结果只有一张表
内查询结果=some()
内查询结果=any()
内查询结果 in()
1)Select 最后要查出来的结果 from 最后要查出来的结果所在表
Where 内查询结果 =(
Select 内查询结果 from 另一张表 where 题目所给的条件(内查询条件)
);
2)当内查询结果是多个值的时候,这个时候需要用in来替换=:
Select 最后要查出来的结果 from 最后要查出来的结果所在表
Where 内查询结果in(
Select 内查询结果 from 另一张表 where 题目所给的条件(内查询条件)
);
3)又或者是在=后面加上some或者 any;
Select 最后要查出来的结果 from 最后要查出来的结果所在表
Where 内查询结果 =some/any(
Select 内查询结果 from 另一张表 where 题目所给的条件(内查询条件)
);
例1: select sname from student
where sno=(select sno from course where cname=‘yuwen’and cpoint>90);
例2:select sname from student where sno =(select sno from course where cpoint >90) or (select sno from student where sage >23);


2、普通连接 --查询出来结果是两张表
Select * from 表1,表2 where 表1.A=表2.A and 题目里面所给的条件;
(* 代表题目最后查询的结果,两个表的连接属性)
例1:select s.sname,c.sno from student s,course.c
where s.sno=c.sno and c.cname=’yuwen’ and c.cpoint>90;
例2: select s.sname,c.sno from student.s,course.c
where s.sno=c.sno and c. cpoint >90 or s.age>23;
3、连接语句 ----join…on
select * from 表1 join 表2 on 表1.A=表2.A where (我们题目给出的条件)你的条件 and 你的另外一个条件;
select * from student join course on student.sno=course.sno where
course.cname=’shuxue’ and course.cpoint>80;

3、聚合函数
1)AVG() –返回指定列的平均值
2) COUNT() --返回指定列中非null值的个数
3) MIN() --返回指定列的最小值
4) MAX() --返回指定列的最大值
5)SUM() --返回指定列的所有值之和

4、navicat转化为sql文件:
右击点击表名—转储sql文件—数据和结构

5、 Group by
例1:select gender ,COUNT(*)from score group by gender;


6、 Group by….having
Select 分组列名,count(*) from 表名 group by 分组列名 having 条件;
例:select name from student group by id having shuxue>80;

7、正则表达式
符号匹配 相似查询
regexp
以s开头:‘^s’;
以n结尾:‘n$’
包含: ‘a’ –包含a的字母
匹配任意所有的(除了\n不能匹配其余都能匹配):‘.’
\n:换行

例:
Andy
Asdy
Csdy
直接匹配:‘dy’ ‘(an|as|cs|)dy’
在选的东西之中选择相同之处,进行提取;


8、索引:
1)Index --查询速度很快
主键是唯一的但不允许有空;
Unique:索引列的值必须唯一,但允许有空值
2)create index 索引名 on 表名(列名(长度));--创建索引
例:create index gender on score (gender);--建议索引名与你要建立的索引的列名保持一致,方便查找
3)alter table 表名 add index 索引名(列名); --添加索引
例:alter table score add index name(name);
4)create table 表名(列名1 数据类型 约束,
列名2 数据类型 约束,
Index 索引名 (列名));--创建表的时候直接指定索引
例:create table xihaifeng(
id int ,name varchar(20),
gender VARCHAR(20),
age int ,
index name(name));
5)drop index 索引名 on 表名; --删除索引名
Alter table 表名 drop index 索引名; --删除索引名
6)create unique index 索引名 on 表名(列名);--创建唯一索引
例:create unique index grade on score(grade);
7)alter table 表名 add unique 索引名 (列名); --增加唯一索引
例:alter table score add unique name(name);
8)create table 表名(列名1 数据类型 约束,
列名2 数据类型 约束,
unique 索引名 (列名));--创建表的时候直接指定唯一索引
例:create table xihaifeng(
id int ,name varchar(20),
gender VARCHAR(20),
age int ,
unique name(name));
9、创建临时表:
例:CREATE TEMPORARY TABLE SalesSummary (
product_name VARCHAR(50) NOT NULL,
total_sales DECIMAL(12,2) NOT NULL DEFAULT 0.00,
avg_unit_price DECIMAL(7,2) NOT NULL DEFAULT 0.00,
total_units_sold INT UNSIGNED NOT NULL DEFAULT 0
); --该表在退出mysql的时候会销毁,如果你想主动删除,步骤和普通表一样。
10、常见命令:
例:select version(); --查看服务器版本信息
       Select database(); --查看当前数据库名(或返回空)
       Select user(); --查看当前用户名
       Show status; --服务器状态
       Show variables; --服务器配置变量
11、mysql的序列化
1)自动增长 --auto_increment
例:create table xihaifeng2(
id int not null auto_increment,
name varchar(20),
unique id(id)
);
insert into xihaifeng2(id,name) VALUES(null,'zhangsan');
注:当id为自动化增长时,可以不输入id地址,插入数据时直接输入null,因为会自动生成;
2)重置序列 --删除了数据表中的多条记录,并希望对剩下数据的auto_increment 列进行重新排列,可以通过删除自增的列,然后重新添加来实现。
例:alter table score drop id; --删除自增长
Alter table score add id int (unsigned) not null auto_increment first,add primary key(id); --添加自增长,并设置为主键,一般情况下,主键为自增长
12、自增长的初始值
例:create table score(
Id int (unsigned) not null auto_increment ,
Primary key (id),
Name varchar (30) not null,
Date date not null,
Origin varchar(30) not null)
Auto_increment =100 charset=utf8; --初始值设置为100;

13、多表查询
1)使用select子句进行多表查询
Select 列名 from 表1,表2 where 表1.A=表2.A and 其他查询条件;
例:select name from student s,teacher t where s.id=t.id and s.age >18;
--两个表进行关联,以两张表的id字段信息相同作为条件建立两表关联
2)合并多个结果集
-- 将多个select语句的查询结果合并输出,并删除重复行;
select id from aaa union (distinct) select id from bbb;
--将多个select语句的查询结果合并输出,但不会删除重复行
select id from aaa union all select id from bbb;
3)简单嵌套查询
内连接:把查询结果作为where子句的查询条件;
--返回单个值且嵌套在select、insert、update、delete语句或其他查询语句中,任何可以使用表达式的地方都可以使用子查询。
例:select name from student where id in(select id from teacher where age=23);
4)多层嵌套:(最多嵌套32层)复杂的select语句查询
Select语句 in{select语句 in{select语句 in{……}}};
--多表嵌套查询的原理:无论是多少张表进行嵌套,表与表之间一定存在某种关联,通过where子句建立此种关联实现查询;
5)嵌套查询的应用:
>any --大于子查询中的某个值
>=any --大于等于子查询中的某个值
<=any --小于等于子查询中的某个值
=any --等于子查询中的某个值
!=any/<>any --不等于子查询中的某个值
>all --大于子查询中的所有值
>=all --大于等于子查询中的所有值
<=all --小于等于子查询中的所有值
=all --等于子查询中的所有值
!=all/<>all --不等于查询中的所有值

内查询结果=some()/内查询结果=any():(关键词or)
假设any内部的查询语句返回的结果个数是三个,那么select* from表名 where a>结果1 or a>结果2 or a>结果3;
内查询结果 in()
内查询结果all():
假设any内部的查询语句返回的结果个数是三个,那么select* from表名 where a>结果1 and a>结果2 and a>结果3;
 Select 最后要查出来的结果 from 最后要查出来的结果所在表
Where 内查询结果 =
Select 内查询结果 from 另一张表 where 题目所给的条件(内查询条件)
);
 当内查询结果是多个值的时候,这个时候需要用in来替换=:
Select 最后要查出来的结果 from 最后要查出来的结果所在表
Where 内查询结果in
Select 内查询结果 from 另一张表 where 题目所给的条件(内查询条件)
);
 又或者是在=后面加上some或者 any;
Select 最后要查出来的结果 from 最后要查出来的结果所在表
Where 内查询结果 =some/any
Select 内查询结果 from 另一张表 where 题目所给的条件(内查询条件)
);
例1: select sname from student
where sno=(select sno from course where cname=‘yuwen’and cpoint>90);
例2:select sname from student where sno =(select sno from course where cpoint >90) or (select sno from student where sage >23);

6)使用子查询做表达式
--子查询的结果作为一个表达式
例:select (select avg(yuwen)from cj),(select avg(shuxue)from cj),(select avg(yingyu)from cj)from cj;
--在使用子查询时最好为列表项取个别名;
例:select (select avg(yuwen)from cj)as yuwen,(select avg(shuxue)from cj) as shuxue,(select avg(yingyu)from cj) as yingyu from cj;

7) join 查询
(inner)join—内连接查询
例:select name from aaa join bbb on aaa.id=bbb.id where score=90;
Left join—左连接 左表aaa是主表,完整的,右表是贴上去的,可以不完整
例:select name from aaa join bbb on aaa.id=bbb.id ;
Right join—右连接 右表bbb是主表,完整的,左表是贴上去的,可以不完整
例:select name from aaa join bbb on aaa.id=bbb.id ;

posted @ 2018-06-11 14:06  Ena0827  Views(212)  Comments(0Edit  收藏  举报