Mysql优化

 

  1. 创建数据库

Create database 数据库名  charset = utf-8

  1. 删除数据库

Drop database 数据库名

  1. 切换数据库

Use  数据库名

  1. 查看当前数据库

Select database();

  1. 查看表

Show  tables

  1. 修改

Update  表名  set 列名=1, .....where 条件

  1. 删除

Delete from 表名  where 条件

  1. 数据库的备份

进入到根目录下  

然后mysqldump -uroot -p 数据库名字 > ~/路径/sql文件名

  1. 数据库的还原

Mysql -uroot -p 数据库名 < sql文件

数据库查询

查询表数据

Select * from 表名

查询不重复的数据

Select distinct 列名 from 表名

比较运算符

And 俩个条件都要满足

Or 满足一个就行

Not  否定

模糊查询

Like

% 匹配多个字符

_匹配一个字符

范围查询:

In  表示在一个不连续的范围内查询

: select * from 表名 where id in(1,3,8)

Between ..  and ..  表示在一个连续的范围

Select * from 表名 where id between 1 and 5

怎么判断为空 select * from 表名 where 列名 is not null.

 聚合函数:

Count(*)   计算总数  select count(*) from 表名

Max 最大值 select max(id) from 表名

Min 最小值 select min(id) from 表名

Sum 列之和

Avg 平均值 select avg(id) from 表名

分组:  Group by

group by Having  追加条件  只是对Group  by 结果进行筛选

排序::order by  

Asc 从小到大排序

Desc 从大到小排序

获取部分行: (也就是分页)  用  limit

Limit 2,1 (2表示从第几个,1表示显示几个)

求第几页的数据: select * from 表名 limit (n-1)*m,n

链接查询:

:

Select student.name, subject.title, scores.score from scores inner

jion students on scrores.stuId=students.id inner jion subjects on scores.subid = subjects.id  

左外链接  匹配左边所有  right   jion ...  on

右外链接  匹配右边所有  left   jion ...   on

 

 

posted on 2018-05-10 17:22  奋斗的小菜鸟+1  阅读(109)  评论(0编辑  收藏  举报