lwq1996 发表于 2021-8-11 21:19:35

8/11 单表与多表

单表语句:

(1)查询一个表中的所有语句
格式: select* from表名;
select*fromhz10

注意:*号表示所有,每个语句后面加上分号

(2)查询表中具体的字段
格式:select字段名1,字段名2,字段名3from表名;
selectnamefromhz10;

(3)查询表中具体的字段取别名(别名是为了在查看数据时方便)
格式:select 字段1 as “姓名” ,字段2 “年龄”   from 表名
selectname as“姓名”,age“年龄” from hz10;
备注:as可以省略不写,as表示取别名

比较运算符
(1)查询指定内容,用where字段名=字段
select * from emp where dept2=101;

where条件使用注意:
比较运算符
where +条件(=(等于),!=(不等于),<>(不等于),<(小于),>(大于),>=(大于等于),<=(小于等于))

(2)and(与)
同时满足所有条件,比如同时满足条件1,条件2
select * from emp where dept2 >=102andage=57;

(3)or(或)
有多个条件时,满足其中任意1个条件都显示,条件1和条件2同时存在,只要满足条件1就显示,只要满足条件2就显示
select * from emp where dept2 =102orage=57;

(4)between .....and.....在什么范围之间
select * from表名 where 表字段 between 值的范围and 值的范围
select * from表名 where age between 25and 55;

(5)in 在一组数据中匹配
格式:select * from 表名 where 字段名 in (数组值1,数组值2,数组值3);
select * from empwhere dept2 in(101,102,103);
(6)isnull

6.1 isnot null 非空
格式:select* from表名 where字段名 is not null;
select* fromemp whereage is not null;

6.2 isnull   为空
格式:select* from表名 where字段名 isnull;

多行注释:ctrl+/
取消多行注释:ctrl+shift+/
单行注释:#
============================================================================

简表讲解

dept部门表
C:/Users/Administrator/AppData/Local/YNote/data/weixinobU7Vjp8N9B-UYSDrAgFp2cOyS1Q/13107f292777492e965d73e641a3864c/5aiws36zy@%29q@5xs8fy@l%60q.png
emp员工表
C:/Users/Administrator/AppData/Local/YNote/data/weixinobU7Vjp8N9B-UYSDrAgFp2cOyS1Q/0220435432d94c49bb9339f1dc393cc4/tz8qv4%60kcrd8mgq_sk7w__f.png

排序:

对表中的内容进行排序:
order by:排序

asc:升序
格式:select*from表名orderby字段名 asc;
案例1:select*fromemporderbyage asc;
案例2:select*fromemporderbyage; (不填asc)


desc:降序
格式:select*from表名orderby字段名 desc;
案例:select*fromemporderbyage desc;

二次排序:
格式:select*from表名 orderby字段1desc ,字段2 desc;
select*fromemporderbydeptdesc ,incoming desc;

模糊匹配查询 like
%:表会匹配0个字符或多个字符
_:表示一个字符

select   * fromempwhere dept2like “1%” ;    匹配1开头的部门编号数据
select   * fromempwhere dept2like “%2%” ;匹配的部门包含2编号数据
select   * fromempwhere dept2like “%2” ; 匹配结尾包含2编号数据
select   * fromempwhere dept2like “_2” ; 匹配固定的字符2编号数据

限制查询(limit)后面连接2个值,第一个值 表示下标(索引),第二个值是步长
备注:一个表中索引是从0开始的
格式:select   *from表名 limit索引,行数;
select   *fromemp limit0,3;从首行开始取3行
select   *fromemp limit2,3;   从第二行开始取3行
select   *fromemp limit3;直接查询3行

分组查询groupby

group by 一般和having组合
案例:selectdept2 ,max(age) fromempgroupbydept2   选出dept2中年龄最大的

group by 一般不会单独使用,通常都是和函数组合使用
group by 后查询出来的结果,在需要的条件下可以接having
案例:selectdept2 ,max(age)assfromempgroupbydept2havings>5;
having+条件 和where+条件用法一样,但是场景不一样,一般group by 的后面接having
备注:函数求出以后,需要设置成别名,设置别名的字段,进行语句的操作

错误案例:
select name,max(age) from emp      默认


max 最大值    selectmax(age) from emp
min最小值    selectmin(age) from emp
avg平均值    selectmin(age) from emp
count 统计总数selectcount(age) from emp
sum 求和    selectsum(age) from emp
distinct 去重selectdistinct(dept2) from emp
===========================================================================
update   _set
格式:表名set   修改字段名=“字段新值”where条件;
updateempsetname=“成权”wheresid=1789;
=========================================================================
删:
delete:删除
truncate:快速删除表内数据
drop:删除
删除数据的速度排名:drop>truncate>delete

注:1、drop是删除表和数据
2、truncate 删除无法恢复
3、delete 删除是可以恢复的

delete:
(1)删除表中所有数据
格式:delete from表名 ;
案例:delete from hz10;

(2)删除表中指定条件数据
格式:deletefrom表名where条件;
案例:delete from empwhere dept2=103;

truncate
(1)快速删除表内数据
格式:truncate 表名
案例:truncatehz10

============================================================================
表取别名:
表名设置别别名
格式
案例:selects.dept2,s.name fromemp ass;()

备份表结构
格式:creae table   新表名 like备份原表名
案例:create table hz110likeemp

备份数据:
insertinto新表 有表结构 select * from 备份表的原表
insertinto汉字10 select *from emp

备份部分数据
insert into 表名(s字段名1,字段名2) select sid,dept2 from emp;
insert into hz110hz110(sid,dept2) select sid,dept2 from emp;

备份表结构和数据:
create table 表名 as(select   * from备份原表)
create table hzdcs110 as(select   * fromemp)
select *from hzdcs110;

linux里(xshell)
备份数据库
格式:mysqldump-uroot-p 原数据库>数据库脚本后缀
案例:mysqldump-uroot-phz10(数据库)>/home/hz10bf.sql
C:/Users/Administrator/AppData/Local/YNote/data/weixinobU7Vjp8N9B-UYSDrAgFp2cOyS1Q/812517248dd64027bb5529870036a6d9/%60gr3w%24c0n%7D8%7E3%7Bn29%7E%240ufq.png

还原数据库:
1、建立一个新的空库
2、在linux中还原 <
3、mysql-uroot-p aa<hzbf.sql
mysql-uroot-p 新建的空库<数据库备份好的数据脚本
C:/Users/Administrator/AppData/Local/YNote/data/weixinobU7Vjp8N9B-UYSDrAgFp2cOyS1Q/3503752b7e5b4e318d044e11d206d3e3/q%7Emi1uo@%29kw1a_7k%28k49pj6.png
================================================================
题目

题目1
查询1719班学生的成绩信息
select name,english,chinese,math from student where class=1719;


题目2
查询1719班学生语文成绩大于80小于90的学生信息
条件:class=179    chinese>80andchinese<90    * 是所有
select * from student where class=1719 AND chinese>80 and chinese<90;

题目3
查询学生表中5-7行的数据信息
所有信息   *
Limit   4(索引) ,3(行数)
SELECT * from student limit 4,3;


题目4
显示1719班英语成绩为90,数学成绩为98的name与mobile信息
Namemobile
条件: class=1719   english=90   math=98
select name,mobile from studentwhere class=1719 and english=90 and math=98;


题目5
显示1719班学生信息并且以语文成绩降序排序

条件class=1719    chinese    desc
select * from student where class=1719 order by chinese desc







题目6
查询1719与1720班,语文成绩与数学成绩都小于80的name与mobile信息

查询:namemobile
条件: class=1719,class=1720    ,chinese<80, math<80

方法一:select NAME,mobile from student2 where class in (1719,1720) andchinese<80 and math<80;
方法二:select NAME,mobile from student2 where(class =1719 or class =1720)andchinese<80 and math<80;
方法三:select NAME,mobile from student2 where(class =1719   andchinese<80 and math<80 ) or (class =1720andchinese<80 and math<80) ;

题目7
查询英语平均分大于80分的班级,英语平均分
显示:班级和平均分      
条件:avg   english>80

select class,avg(english) as aa from student group by class having aa>80;




题目8
按班级查出数学最高分
SELECTclass,max(math) from student2 group by class;



题目9
查询出每班数学最低分
select class,min(math) from student GROUP BY class;


题目10
查询每班数学总分
select class,sum(math) from student GROUP BY class;

题目11
查询每班数学平均分
select class,avg(math) from student GROUP BY class;


题目12
查询出每班学生总数
select class,count(name) from student GROUP BY class;

题目13
在表中插入一条小谢的成绩数据
insert into student values(8,'小谢','女',23,138138288,1719,77,88,98);
题目14
把语文分数小于60的同学分数改为60分
update student set chinese=80 WHERE chinese<80;


============================================================================
多表:
1、什么是多表关联查询?
定义:查询数据来源于多张表

主要讲:1,内连接(基本内连接和隐藏内连接)
               2,左连接(左外连接)
               3,右连接(右外连接)
               4,全连接(全外连接)

C:/Users/Administrator/AppData/Local/YNote/data/weixinobU7Vjp8N9B-UYSDrAgFp2cOyS1Q/8269fba6cacb4b34a70a8339676ef951/%25@al%5Bdf7o%7B0wn%60%60n48%7Dbbb5.jpg
======================================================================


1讲解过程中先建两个表 dept 和 emp
dept表:
dept1 部门编号
dept_name部门名称
C:/Users/Administrator/AppData/Local/YNote/data/weixinobU7Vjp8N9B-UYSDrAgFp2cOyS1Q/3f247dd5e7f54a269f48d0e09b052bca/cpj7vtl%60l@2ck%25935%246o8kp.png
emp表:
sid 员工编号
name 员工姓名
age 员工年龄
worktime_start入职时间
incoming 薪资
dept2 部门编号
C:/Users/Administrator/AppData/Local/YNote/data/weixinobU7Vjp8N9B-UYSDrAgFp2cOyS1Q/7f3d4d80f21748ce8ff5fbe642f7b4b0/clipboard.png

关联关系dept1=dept2
======================================================================
1笛卡尔积查询(了解)
根据两张表相乘得到结果,比如:左边有4条数据,右边有6条数据,查询出来就是4*6=24条数据,这种数据包含大量错误数据,通常不会使用这种查询方法

格式:select * from表1 表2;
案例:select * fromdept ,emp

2内连接(普通内连接,隐藏内连接)
查询两个表共有的关联数据。
C:/Users/Administrator/AppData/Local/YNote/data/weixinobU7Vjp8N9B-UYSDrAgFp2cOyS1Q/756b89cf01aa4402bea101b2ebe8512c/%249lspd%7E3xm%28v9x45%7D%7D%24gf34.png
2.1普通内连接:
格式:select   *from表1    inner join 表2 on 表1.关联字段1=表2.关联字段2
案例1:select   *fromdeptinner join emp on dept.dept1=emp.dept2 ;
案例2:select   name,dept_namefromdeptinner join emp on dept.dept1=emp.dept2where sid=1789 ;


2.2隐藏内连接
格式:select   *from表1,表2   where   表1.关联字段1=表2.关联字段2
select   *fromdept,emp where   dept.dept1=emp.dept2 ;


3、左连接
以左表为主(显示完整的左表),右表关联的数据就显示,没有的数据就以null显示
格式:
select   *from表1leftjoin表2 on       表1.关联字段1=表2.关联字段2 ;
案例:
select   *fromdeptleftjoin emp on dept.dept1=emp.dept2   ;

4、右连接
右表以右表数据显示为主(显示整个右表)左表有关联的数据就显示,没有就以null值显示。
格式:select   *from表1right   join表2 on       表1.关联字段1=表2.关联字段2 ;
select*fromdeptright join emp on dept.dept1=emp.dept2   ;

5、左表独有数据
(显示整个右表)左表有关联的数据就显示,没有就以null值显示。
格式:select   *from表1right   join表2 on       表1.关联字段1=表2.关联字段2 ;
select*fromdeptright join emp on dept.dept1=emp.dept2   ;
6、右表独有数据
右表中独有的数据显示,(方法,右独有,以左表字段为空查询)
格式:select   *from表1    right   join表2 on       表1.关联字段1=表2.关联字段2   where   左表字段isnull
案例:
select   *fromdeptrightjoin emp on dept.dept1=emp.dept2   wheredept1isnull;
7、左表独有数据+右表独有数据
union 拼接
左独有 union 右独有
select   *fromdeptleftjoin emp on dept.dept1=emp.dept2   where   name isnull
union
select   *fromdeptrightjoin emp on dept.dept1=emp.dept2   wheredept1isnull;

8、全连接
方法1:内连接+左独有+右独有
select   *fromdeptleftjoin emp on dept.dept1=emp.dept2   where   name isnull
union
select   *fromdeptrightjoin emp on dept.dept1=emp.dept2   wheredept1isnull
UNION
select   *fromdept    innerjoinemp on dept.dept1=emp.dept2   ;
方法2:左连接+右独有
select   *fromdeptleftjoin emp on dept.dept1=emp.dept2UNION
select   *fromdeptrightjoin emp on dept.dept1=emp.dept2   wheredept1isnull ;
方法3:有链接+左独有
select   *fromdeptright join emp on dept.dept1=emp.dept2   UNION
select   *fromdeptleftjoin emp on dept.dept1=emp.dept2where   name isnull ;

总结:
普通内连接:
select*from表1innerjoin    表2on   表1 的关联字段=表2的关联字段 ;
隐藏内连接:
select*from表1 ,表2where    表1 的关联字段=表2的关联字段 ;
左连接
select*from表1left   join    表2on   表1 的关联字段=表2的关联字段 ;
右连接:
select*from表1rightjoin    表2on   表1 的关联字段=表2的关联字段 ;

左独有数据:
select*from表1left   join    表2on   表1 的关联字段=表2的关联字段   where右表字段为空;

右独有数据:
select*from表1right   join    表2on   表1 的关联字段=表2的关联字段   where   左表字段为空;

全外连接:
1、左独有+右独有+内连接
2、左连接+右独有
3、右连接+左独有



页: [1]
查看完整版本: 8/11 单表与多表