Java 核心技术机器学习·Python·算法MySQL

MySQL基本操作

2017-08-31  本文已影响25人  Albert_Sun

SQL语句:结构化查询语言

1. DDL

1.1 数据库

SHOW DATABASES: 查看所有数据库
USE 数据库名:切换到某数据库
CREATE DATABASE [IF NOT EXISTS] 数据库名 [CHARSET=UTF8]
DROP DATABASE [IF EXISTS] 数据库名

1.2 表

SHOW TABLES: 查看数据库下所有表
DESC 表名:查看表结构
CREATE TABLE [IF NOT EXISTS] 表名 (列名 列类型, 列名 列类型, ......);
DROP TABLE  表名
* 修改表:前缀 ALTER TABLE 表名, alter table 表名 add|change|drop 列名 类型;
1. 添加列: ADD (列名 列类型, 列名 列类型, ......)
2. 删除列: DROP 列名
3. 修改列类型: MODIFY 列名 列类型
4. 修改列名: CHANGE 原列名 新列名 列类型
5. 修改表名: RENAME TO 新表名

2. DML

2.1 插入数据

INSERT INTO 表名

2.2 修改数据

UPDATE 表名 SET ... [where ...]

2.3 删除数据

DELETE FROM 表名 [where ...]

3. DQL

3.1 基本查询

select * from 表名 [where ...]
select 列名1[,  列名2, ...] from 表名  [where ...]
select distinct * from 表名 [where ...] : 去重
select 列名 * 1.5 from 表名 [where ...]: 字符串被当做0
select 列名1 + 列名2 from 表名 [where ...]: NULL与任何运算都为NULL

3.2 条件控制

3.2.1 比较运算符
select * from students where id > 3; 
select * from subjects where id <= 4;
select * from students where sname != '黄蓉';
select * from students where isdelete = 0;
3.2.2 逻辑运算符
select * from students where id>3 and gender=0;
select * from students where id<4 or isdelete=0;
3.2.3 模糊查询
select * from students where sname like '黄%'; 查询姓黄的学生
select * from students where sname like '黄_'; 查询姓黄并且名字是一个字的学生
select * from students where sname like '黄%' or sname like '%靖%'; 查询姓黄或叫靖的学生
3.2.4 范围查询
select * from students where id in(1,3,8); 查询编号是1或3或8的学生
select * from students where id between 3 and 8; 查询学生是3至8的学生
select * from students where id between 3 and 8 and gender=1; 查询学生是3至8的男生
3.2.5 空判断
select * from students where hometown is null;查询没有填写地址的学生
select * from students where hometown is not null; 查询填写了地址的学生
select * from students where hometown is not null and gender=0; 查询填写了地址的女生
3.2.6 优先级

3.3 聚合

select count(*) from students;查询学生总数
select max(id) from students where gender=0;查询女生的编号最大值
select min(id) from students where isdelete=0;查询未删除的学生最小编号
select sum(id) from students where gender=1;查询男生的编号之和
select avg(id) from students where isdelete=0 and gender=0;查询未删除女生的编号平均值

3.4 分组

按照字段分组,表示此字段相同的数据会被放到一个组中
分组后,只能查询出相同的数据列,对于有差异的数据列无法出现在结果集中
可以对分组后的数据进行统计,做聚合运算

select 列1,列2,聚合... from 表名 group by 列1,列2,列3...
select gender as 性别, count(*) from students group by gender; 查询男女生总数
select hometown as 家乡,count(*) from students group by hometown;查询各城市人数
select 列1,列2,聚合... from 表名 group by 列1,列2,列3... having 列1,...聚合...
1. select count(*) from students where gender=1; 查询男生总人数
2. select gender as 性别,count(*) from students group by gender having gender=1;

对比where与having:
where是对from后面指定的表进行数据筛选,属于对原始数据的筛选
having是对group by的结果进行筛选

3.6 排序

将行数据按照列1进行排序,如果某些行列1的值相同时,则按照列2排序,以此类推
默认按照列值从小到大排列
asc从小到大排列,即升序
desc从大到小排序,即降序

select * from 表名 order by 列1 asc|desc, 列2 asc|desc,...
select * from students where gender=1 and isdelete=0 order by id desc;查询未删除男生学生信息,按学号降序
select * from subject where isdelete=0 order by stitle;查询未删除科目信息,按名称升序

3.7 分页

已知:每页显示m条数据,当前显示第n页, 求总页数:
查询总条数p1
使用p1除以m得到p2
如果整除则p2为总数页
如果不整除则p2+1为总页数
求第n页的数据

select * from 表名 limit start,count
select * from students where isdelete=0 limit (n-1)*m,m
上一篇 下一篇

猜你喜欢

热点阅读