找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
单表:
(1)查询一个表中的所有数据
格式:select * from 表名;(——显示表格结果)
eg:select * from hz13;
注意:*号表示所有,每个语句后面加上分号

(2)查询表中表中具体的字段
格式:select 字段名1,字段名2,字段名3 FROM 表名;
eg:select name,age FROM hz13;
select   name,age  FROM   hz13 ;

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


(4)查询指定内容,用where+条件
格式:select  *   from   表名   where  字段名=字段 ;
select  * from   emp   where  dept2=101 ;

where条件使用注意:
比较运算符
(1)where  +条件(=(等于), !=(不等于) ,<>(不等于) ,<(小于),>(大于),>=(大于等于),<=(小于等于))
select  * from   emp   where  dept2=101 ;
select  * from   emp   where  dept2 !=101
select  * from   emp   where  dept2 <>101 ;
select  * from   emp   where  dept2 >102 ;
select  * from   emp   where  dept2 <102 ;
select  * from   emp   where  dept2 <=102 ;
select  * from   emp   where  dept2 >=102 ;
(2)and (与)
and  同时满足所有条件,比如:条件1和条件2都要满足
案例:select  * from   emp   where  dept2 >=102  and age=57 ;
(3)or(或)
or 当有多个条件时满足其中任意1个条件都显示,,条件1和条件同时存在,只要满足条件1就显示,只要满足条件2就显示。
select  * from   emp   where  dept2 =102  or age=55 ;
(4)between..... and....  在什么范围之间
格式:select  *   from   表名  where  表字段 BETWEEN 值得范围    and    值的范围
select  * from   emp   where  age BETWEEN 24 and  55 ;
(5)in  在一组数据中匹配数据
格式:select  * from   表名    where    字段名 in (数组值1,数组值2,数组值3);
select  * from   emp    where  dept2 in (101,102,110);

(6)is  null 为空  或is  not  null  非空

6.1  is  not  null  非空
格式:select  * from   表名    where  字段名  is not null ;
select  * from   emp    where  age  is not null ;
6.2  is    null  为空
格式:
select  * from   表名    where  字段名  is     null ;

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

排序:  

对表中的数据进行拍讯:order  by        asc 升序(可以省略)    desc   降序

升序:

格式:select  * from   表名   order  by  字段名   asc ;

案例1:select  * from   emp   order  by  age asc ;

案例2:select  * from   emp   order  by  age asc ;(不填asc)

降序:

格式:

select  *  from   表名   order  by  字段名    desc ;

案例:select  * from   emp   order  by  age desc ;



二次排序:

格式:select  * from   表名  order  by   字段1 desc ,字段2   asc ;

select  * from   emp   order  by  dept2 desc , incoming asc ;


模糊匹配查询  like

%:表会匹配0个字符或多个字符

—:表示一个字符

select  * from   emp  where  dept2 like "1%"  #匹配1开头的部门编号数据
select  * from   emp  where  dept2 like "%2%" #匹配的部门包含2编号数据
select  * from   emp  where  dept2 like "%2" #匹配结尾包含2编号数据
select  * from   emp  where  dept2 like "__2" #匹配固定的字符2编号数据


限制查询(limit)  后面接连个值  ,第一个值  表示下标(索引),  第二值是步长

备注:一个表中索引是从0开始

格式:select  *   from   表名   limit  索引,行数 ;

案例1:select  * from   emp   limit  2,3 ;

案例2:select  * from   emp   limit  2 ;    直接查询两行



分组查询 group  by

group  by    一般和having   组合

案例:SELECT  dept2,max(age) from   emp  group  by  dept2 ;

SELECT  dept2,max(age) from   emp  group  by  dept2 ;

SELECT  dept2,max(age) from   emp  group  by  dept2 ;

group by  一般不会单独使用个,通常都是和函数组合使用。

gtoup  by 后查询出来的结果,在需要的条件下可以接having

案例:SELECT  dept2 ,max(age)  as  s from   emp  group  by  dept2 HAVING  s>50 ;

having +条件  和where  +条件  用法一样,但是场景不一样,一般group   by 的后面接having

备注:函数求出以后,需要设置成别名,设置别名的字段,进行语句的操作。

错误案例:
(1)SELECT  name,max(age) from   emp

(2)SELECT  name,max(age) from   emp GROUP BY  dept2
E:/noteyoudao/qqC00975CCA04FE5F25136875459BFDA42/32c13ff321b04e66a612ad739cbd8196/clipboard.png

sql语句函数:

max  最大值

案例:SELECT  dept2,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  去重

案例:SELECT  distinct(dept2) from   emp

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

改:

update    ......set   改

格式:update   表名  set   修改的字段名=字段新值   where  条件 ;

案例:update   emp  set   name="成全"  where  sid=1789 ;

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

删:

delete   删

truncate   快速删除表内数据

drop  删除

删除数据速度 :drop  >  truncate  >delete

注意:

1、drop  是删除表和数据

2、truncate 删除无法恢复

3、delete  删除是可以恢复



delete:

(1)删除表中所有数据

格式:delete  from  表名:

案例:delete  from  hz13;

(2)删除表中指定条件的数据

格式: delete  from  表名  where  条件

案例: delete  from  emp  where dept2=103;

(3)快速删除表数据

格式:truncate  表名;

案例:truncate  hz15 ;

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

表取别名:

格式: 表名设置别名,

案例:SELECT  s.dept2,s.name from   emp as s ;  (把emp表设置成临时表s)

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

备份表结构:

格式:create  table  新表名 like  备份原表名 ;

create  table  hz110 like  emp ;



备份数据:

insert  into    新表 有表结构   select   * from   备份表的原表

案例:INSERT  into   hz110  select  *  from  emp ;



备份部分数据:

格式:INSERT  into  表名(s字段名1,字段名2)  select  sid ,dept2  from  emp ;

INSERT  into   hz110110(sid,dept2)  select  sid ,dept2  from  emp ;



备份表结构和数据:

格式:create  table 表名 as(select  * from   备份原表 )

案例:create  table  hzdcs110 as(select  * from   emp )



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

linux备份数据库:

备份用:>

格式:mysqldump -u  root -p 原数据库>数据库脚本.后缀名

案例:mysqldump -u  root -p hz10>/home/hz10bf.sql
E:/noteyoudao/qqC00975CCA04FE5F25136875459BFDA42/65e49d5bdb6d42a58e00952b6e48eec1/clipboard.png
还原数据库:

1、先建立一个新的空库

2、在linux中还原<

3、还原:mysql -u root  -p 新建的空库<数据库备份好的数据脚本

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

主要讲:1、内连接(基本内连接和隐藏内敛及)
2、左连接(左外连接)
3、右连链(右外连接)
4、全链接(全外连接)
============================================================================
E:/noteyoudao/qqC00975CCA04FE5F25136875459BFDA42/a2819561a7ec40a9aee396a0c9c359fa/%25@al%5Bdf7o%7B0wn%60%60n48%7Dbbb5.jpg
============================================================================
1、讲解过程先建两个表 :dept表 和 emp 表
dept表:
dept1部门编号
dept_name  部门
E:/noteyoudao/qqC00975CCA04FE5F25136875459BFDA42/7dc59a4090914e539207dd506c8b6e98/cpj7vtl%60l@2ck%25935%246o8kp.png
emp表:
sid    员工编号
name    员工姓名
age        员工年龄
worktime_start     入职时间
incoming     员工收入
dept2      部门编号
E:/noteyoudao/qqC00975CCA04FE5F25136875459BFDA42/733e59420e984c85a7cfb07fe598acf9/clipboard.png



关联关系:dept1 =dept2
============================================================================
1、笛卡尔积查询(了解)
根据两张表所乘得到的结果:比如:左边有4条数据,右边有6条数据,查询出来就是4*6=24条数据,这种查询数据包含大量的错误结果,通常不会使用这种查询。
格式:select * from 表1,表2;
案例:select * from  dept,emp;

2、内连接(普通内连接,隐藏内连接)
查询两个表共有的关联数据。
  • 2.1普通内连接:

格式:select   *  from  表1    inner join 表2 on 表1.关联字段1=表2.关联字段2

案例1:select   *  from  dept  inner join emp on dept.dept1=emp.dept2 ;
案例2:select   name,dept_name  from  dept  inner join emp on dept.dept1=emp.dept2  where sid=1789 ;


2.2隐藏内连接

格式:select   *  from  表1,表2   where   表1.关联字段1=表2.关联字段2

select   *  from  dept,emp where   dept.dept1=emp.dept2 ;

3、左连接

以左表为主(显示完整的左表),游标关联的数据就显示,没有的数据就以null显示

格式:
select   *  from  表1  left  join  表2 on       表1.关联字段1=表2.关联字段2 ;

案例:
select   *  from  dept  left  join emp on dept.dept1=emp.dept2   ;



4、右连接

右表是以右表数据为主(显示整个右表)左表有关联的数据就显示,没有就以null值显示。

格式:select   *  from  表1  right   join  表2 on       表1.关联字段1=表2.关联字段2 ;

select*  from  dept  right join emp on dept.dept1=emp.dept2   ;



5、左表独有数据

左表中独有的数据显示,(方法,左独有,以右表字段为空查询)

格式:select   *  from  表1  left  join  表2 on       表1.关联字段1=表2.关联字段2   where     右表字段  is  null  

select   *  from  dept  left  join emp on dept.dept1=emp.dept2   where   name is  null;



5、右表独有数据

右表中独有的数据显示,(方法,右独有,以左表字段为空查询)

格式:select   *  from  表1    right     join  表2 on       表1.关联字段1=表2.关联字段2   where     左表字段  is  null  

案例:
select   *  from  dept  right  join emp on dept.dept1=emp.dept2   where  dept1  is  null;



6、左表独有数据+右表独有数据:

union  拼接

左独有  union  右独有

select   *  from  dept  left  join emp on dept.dept1=emp.dept2   where   name is  null
union
select   *  from  dept  right  join emp on dept.dept1=emp.dept2   where  dept1  is  null;



7、全连接

方法一:左独有+右独有+内连接

select   *  from  dept  left  join emp on dept.dept1=emp.dept2   where   name is  null
union
select   *  from  dept  right  join emp on dept.dept1=emp.dept2   where  dept1  is  null
UNION
select   *  from  dept    inner  join  emp on dept.dept1=emp.dept2   ;





方法二:左连接+右独有

select   *  from  dept  left  join emp on dept.dept1=emp.dept2  UNION
select   *  from  dept  right  join emp on dept.dept1=emp.dept2   where  dept1  is  null ;



方法三:右连接+左独有

select   *  from  dept  right join emp on dept.dept1=emp.dept2   UNION

select   *  from  dept  left  join emp on dept.dept1=emp.dept2  where   name is  null ;



总结:

普通内连接:

select  *  from  表1  inner  join    表2  on   表1 的关联字段=表2的关联字段 ;

隐藏内连接:

select  *  from  表1 ,  表2  where    表1 的关联字段=表2的关联字段 ;

左连接

select  *  from  表1  left   join    表2  on   表1 的关联字段=表2的关联字段 ;

右连接:
select  *  from  表1  right  join    表2  on   表1 的关联字段=表2的关联字段 ;



左独有数据:

select  *  from  表1  left   join    表2  on   表1 的关联字段=表2的关联字段   where  右表字段为空;



右独有数据:

select  *  from  表1  right   join    表2  on   表1 的关联字段=表2的关联字段   where   左表字段为空;



全外连接:

1、左独有+右独有+内连接

2、左连接+右独有

3、右连接+左独有


分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册