一、 一层嵌套子查询 --查询出来结果只有一张表
内查询结果=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);mysql
二、普通链接 --查询出来结果是两张表
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;
三、链接语句 ----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;正则表达式
三、聚合函数
1)AVG() –返回指定列的平均值
2) COUNT() --返回指定列中非null值的个数
3) MIN() --返回指定列的最小值
4) MAX() --返回指定列的最大值
5)SUM() --返回指定列的全部值之和sql
四、navicat转化为sql文件:
右击点击表名—转储sql文件—数据和结构数据库
五、 Group by
例1:select gender ,COUNT(*)from score group by gender;服务器
六、 Group by….having
Select 分组列名,count(*) from 表名 group by 分组列名 having 条件;
例:select name from student group by id having shuxue>80;函数
七、正则表达式
符号匹配 类似查询
regexp
以s开头:‘^s’;
以n结尾:‘n$’
包含: ‘a’ –包含a的字母
匹配任意全部的(除了\n不能匹配其他都能匹配):‘.’
\n:换行spa
例:
Andy
Asdy
Csdy
直接匹配:‘dy’ ‘(an|as|cs|)dy’
在选的东西之中选择相同之处,进行提取;regexp
八、索引:
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));
九、建立临时表:
例: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的时候会销毁,若是你想主动删除,步骤和普通表同样。
十、常见命令:
例:select version(); --查看服务器版本信息
Select database(); --查看当前数据库名(或返回空)
Select user(); --查看当前用户名
Show status; --服务器状态
Show variables; --服务器配置变量
十一、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); --添加自增加,并设置为主键,通常状况下,主键为自增加
十二、自增加的初始值
例: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;索引
1三、多表查询
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 --不等于查询中的全部值rem
内查询结果=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 ;