段志伟 发表于 2021-8-11 21:19:12

mysql之基本查询语句---单表查询-----多表查询

单表语句:
1、查询一个表中所有的数据
select * from表名;
例:select *fromhz10   
注:* 表示所有 每个语句后面加上分号

2、查询表中具体的字段
select字段名1,字段名2   from表名;
例:select   name,age   fromhz10 ;

3、查询表中具体的字段取名(别名是为了查看数据方便)
selectnameas “姓名”,age“年龄”fromhz10;
注:as可以省略不写as表示取别名

4、查询指定内容,用where+条件
select   *from表名where    字段名=字段
例:select*fromempwhere   dept2=101;

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

and(与)同时满足所有条件
例:select*frromhz10wheretz=多少and   sg=多少;

or(或)有多个条件时满足其中一个

between.......and.............(在什么范围之间)
例:select * from 表名where    表字段   between   值的范围   and   值得范围;

in(在一组数据中匹配)从一组数据中匹配已有数据
select   *   from   表名   where字段名 in(数组数据);

is null(为空)或者 is not nulll (非空)
select*from   表名   where   +字段名+   条件;

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


排序:
对表中的数据进行排序:orderby      ass 升序(可以省略) desc降序
升序:
格式:select*from   表名   orderby    字段名   asc;

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


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


模糊匹配查询 like
%:表示匹配0个字符或多个字符
_:表示一个字符
例:
select* from   empwheredept2 like "1%"#匹配1开头的部门编号数据
select* from   empwheredept2 like "%2%" #匹配的部门包含2编号数据
select* from   empwheredept2 like "%2" #匹配结尾包含2编号数据
select* from   empwheredept2 like "__2" #匹配固定的字符2编号数据

限制查询(limit)后面接两个值
第一个值 表示下标(索引),第二个值是步长
注:一个表的索引是从0开始
格式:select * from 表名limit索引,行数;


分组查询 groupby
groupby和 having组合
案例:
SELECTdept2,max(age) from   empgroupbydept2 ;
group by一般不会单独使用个,通常都是和函数组合使用。
gtoupby 后查询出来的结果,在需要的条件下可以接having
案例:SELECTdept2 ,max(age)ass from   empgroupbydept2 HAVINGs>50 ;
having +条件和where+条件用法一样,但是场景不一样,一般group   by 的后面接having
备注:函数求出以后,需要设置成别名,设置别名的字段,进行语句的操作。
错误案例:
(1)SELECTname,max(age) from   emp
(2)SELECTname,max(age) from   emp GROUP BYdept2
C:\Users\dzw\AppData\Local\YNote\data\qq2CE6E06613DFC0686050CB26675F52A3\01469f7cefe14701852d0791857288a2\08b7333b5444.png

sql语句函数:
max最大值
案例:SELECTdept2,max(age) from   emp
min最小值
案例:SELECT min(age) from   emp
avg 平均值
案例:SELECT avg(age) from   emp
count统计总数
案例:SELECT count(age) from   emp
sum求和
案例:SELECT sum(age) from   emp
distinct去重
案例:SELECTdistinct(dept2) from   emp
====================================
改:
update    ......set   改
格式:update   表名set   修改的字段名=字段新值   where条件 ;
案例:update   empset   name="成全"wheresid=1789 ;
====================================
删:
delete   删
truncate   快速删除表内数据
drop删除
删除数据速度 :drop>truncate>delete
注意:
1、drop是删除表和数据
2、truncate 删除无法恢复
3、delete删除是可以恢复

delete:
(1)删除表中所有数据
格式:deletefrom表名:
案例:deletefromhz13;
(2)删除表中指定条件的数据
格式: deletefrom表名where条件
案例: deletefromempwhere dept2=103;
(3)快速删除表数据
格式:truncate表名;
案例:truncatehz15 ;
====================================
表取别名:
格式: 表名设置别名,
案例:SELECTs.dept2,s.name from   emp as s ;(把emp表设置成临时表s)
====================================
备份表结构:
格式:createtable新表名 like备份原表名 ;
createtablehz110 likeemp ;

备份数据:
insertinto    新表 有表结构   select   * from   备份表的原表
案例:INSERTinto   hz110select*fromemp ;

备份部分数据:
格式:INSERTinto表名(s字段名1,字段名2)selectsid ,dept2fromemp ;
INSERTinto   hz110110(sid,dept2)selectsid ,dept2fromemp ;

备份表结构和数据:
格式:createtable 表名 as(select* from   备份原表 )
案例:createtablehzdcs110 as(select* from   emp )

====================================
linux备份数据库:
备份用:>
格式:mysqldump -uroot -p 原数据库>数据库脚本.后缀名
案例:mysqldump -uroot -p hz10>/home/hz10bf.sql
C:\Users\dzw\AppData\Local\YNote\data\qq2CE6E06613DFC0686050CB26675F52A3\82233f533b4145b19f9573afe73edc21\bb89b7868d5b.png

还原数据库:
1、先建立一个新的空库
2、在linux中还原<
3、还原:mysql -u root-p 新建的空库<数据库备份好的数据脚本





================================
建表讲解:
dept 部门表
C:\Users\dzw\AppData\Local\YNote\data\qq2CE6E06613DFC0686050CB26675F52A3\c7ca32d8f8444f689c746affb0efef13\0befd013b46e.png
emp 员工表
C:\Users\dzw\AppData\Local\YNote\data\qq2CE6E06613DFC0686050CB26675F52A3\aa8df112ed804f0c973875c624748e55\d30e4f992380.png

=======================================

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

1、内连接(基本内连接与隐藏内连接)
2、左链接(左外链接)
3、右链接(右外链接)
4、全链接(全外链接)
C:\Users\dzw\AppData\Local\YNote\data\qq2CE6E06613DFC0686050CB26675F52A3\48805ad5ad054bf6ba9fd5df08feddc7\7b14216a5220fc5ee4733ee3143afb4.png


1、笛卡尔积查询(了解)
根据两张表想乘得到的结果,包含大量错误结果,通常不会使用
格式:seleect * from 表1,表2;

2、内连接
格式:select * from 表1 inner join 表2 on关联数据=关联数据;
例:select * fromdeptinner join   emp on dept.dept1=emp.dept2;

隐藏内链接:
格式:select * from 表1,表2 where 关联字段=关联字段;

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

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

5、左表独有数据
左表中独有的数据显示,(方法,左独有,以右表字段为空查询)
格式:select * from 表1left join 表2 on关联数据=关联数据 where 右表字段 is null;
例:select * fromdept leftjoin   emp on dept.dept1=emp.dept2 wherename is null;

5、右表独有数据
右表中独有的数据显示,(方法,右独有,以左表字段为空查询)
格式:select * from 表1right join 表2 on关联数据=关联数据 where 左表字段 is null;
例:select * fromdept rightjoin   emp on dept.dept1=emp.dept2 wherename is null;

6、左表独有数据+右表独有数据:
union 拼接
左独有 union 右独有

7、全链接
“+”=union
方法一:内连接+左独有+右独有
方法二:左链接+右独有
方法三:右链接+左独有

总结:
普通内连接:select * from 表1 inner join 表2 on表1的关联字段=表2的关联字段;
隐藏内连接:select * from 表1,表2 where 表1的关联字段=表2的关联字段;
左连接:select * from 表1left join 表2 on表1的关联字段=表2的关联字段;
右链接:select * from 表1right join 表2 on表1的关联字段=表2的关联字段;
左独有:select * from 表1left join 表2 on表1的关联字段=表2的关联字段 where 右表字段 is null;
右独有:select * from 表1right join 表2 on表1的关联字段=表2的关联字段 where 左表字段 is null;

页: [1]
查看完整版本: mysql之基本查询语句---单表查询-----多表查询