mysql用的最多的是查询操作,下面就总结一下mysql的常见查询语句。
基本查询结构:
-- 查询
默认数据库文件存放在: /var/lib/mysql
-- 数据库的操作
# 在linux终端:
- 启动:$ sudo service mysql start
- 停止:$ sudo service mysql stop
- 重启:$ sudo service mysql restart
-- 链接数据库
-- mysql -uroot -p密码
-- mysql -uroot -p
-- sql语句最后需要有分号;结尾
-- 显示数据库版本
select version();
-- 显示时间
select now();
-- 查看所有数据库
show databases;
-- 创建数据库
-- create database 数据库名 charset=utf8;
create database python_new_info;
create database python_new_info charset=utf8;
-- 查看创建数据库的语句
show create database python_new_info;
-- 查看当前使用的数据库
select database();
-- 使用数据库
-- use 数据库的名字
use python_new_info
-- 删除数据库
-- drop database 数据库名;
drop database python_new_info;
-- 数据表的操作
-- 查看当前数据库中所有表
show tables;
-- 创建表
-- auto_increment表示自动增长
-- 创建一个学生的数据表(id、name、age、high、gender、cls_id)
-- create table 表名(字段名 类型名 约束);
create table classes(
id int unsigned auto_increment primary key not null,
name varchar(20) not null
);
create table students (
id int unsigned auto_increment primary key not null,
name varchar(30) not null,
age tinyint unsigned default 0,
high decimal(5,2),
gender enum("男", "女", "中性", "保密") default "保密",
cls_id int unsigned
);
-- 查看表的创建语句
show create table classes;
show create table students;
-- 查看表结构
-- desc 数据表的名字;
desc students;
+--------+-------------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------------------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(30) | NO | | NULL | |
| age | tinyint(3) unsigned | YES | | 0 | |
| high | decimal(5,2) | YES | | NULL | |
| gender | enum('男','女','中性','保密') | YES | | 保密 | |
| cls_id | int(10) unsigned | YES | | NULL | |
+--------+-------------------------------------+------+-----+---------+----------------+
--
修改表-添加字段
-- alter table 表名 add 列名 类型;
alter table students add birthday datetime;
-- 修改表-修改字段:不重命名版
-- alter table 表名 modify 列名 类型及约束;
alter table students modify birthday date;
-- 修改表-修改字段:重命名版
-- alter table 表名 change 原名 新名 类型及约束;
alter table students change birthday birth date;
-- 修改表-删除字段
-- alter table 表名 drop 列名;
alter table students drop high;
-- 删除表
-- drop table 表名;
drop table students;
-- 增删改查(curd)
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
+-------+------------------+------+-----+---------+----------------+
+--------+-------------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------------------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(30) | NO | | NULL | |
| age | tinyint(3) unsigned | YES | | 0 | |
| gender | enum('男','女','中性','保密') | YES | | 保密 | |
| cls_id | int(10) unsigned | YES | | NULL | |
| birth | date | YES | | NULL | |
+--------+-------------------------------------+------+-----+---------+----------------+
-- 增加
-- 全列插入
-- insert into 表名 values (...)
insert into classes values(0, "python_01");
insert into students values(0, "张三", 20, "男", 1, "1990-01-01");
-- 失败 insert into students values(0, "李四", 20, "不知道", 1, "1990-01-01");
insert into students values(0, "李四", 20, 1, 1, "1990-01-01");
-- 失败 insert into students values(0, "王二", 20, 5, 1, "1990-01-01");
-- 部分插入
-- insert into 表名(列1,...) values(值1,...)
insert into students (name) values ("李四");
insert into students (name, birth) values ("王二", "2008-08-08");
-- 多行插入
insert into students (name, birth) values ("刘备", "1000-08-08"),("张飞", "1000-08-08");
insert into students values (0, "张三1", 20, "男", 1, "1990-01-01"), (0, "张三2", 20, "男", 1, "1990-01-01");
-- 修改
-- update 表名 set 列1=值1,列2=值2... where 条件
update students set age=21;
update students set age=18 where id=1;
update students set age=10,gender=4 where id=1;
-- 删除
-- 物理删除
-- delete from 表名 where 条件;
delete from students where id=2;
-- 逻辑删除
-- 用一个字段来表示 这条信息是否已经不能再使用了
alter table students add is_delete bit;
alter table students modify is_delete bit default 0;
update students set is_delete=0;
update students set is_delete=1 where id=5;
-- 查询基本使用
-- 查询所有列
-- select * from 表名;
select * from students;
-- 指定条件查询
-- select * from 表名 where xxxx;
select * from students where is_delete=1;
-- 查询指定列
-- select 列1,列2,... from 表名;
select name, gender from students;
-- 字段的顺序
select gender, name from students;
-- 可以使用as为列或表指定别名
-- select 列1【 as 名字】,列2【 as 名字】,... from 表名;
select name as 姓名, gender as 性别 from students;
-- 数据库的备份
在mysql 后台服务器开启的状态下: (linux终端)
$ mysqldump -uroot -p 数据库名 > 数据库名.sql # 导出备份,导出到当前文件夹下。root 为用户名
# 从外界导入列表到数据库
$ mysqldump -u用户名 -p 数据库名 < 数据库名.sql
# 在数据库内部导入外界当前登录路径下的列表数据
sourece sql文件名
在数据库内部添加.sql 文件-----> source stock_db.sql
进入数据库再输入,注意源文件一定要在登录数据库时的路径下。
查询:
-- 查询所有字段
-- select * from 表名;
select * from students;
-- 查询指定字段
-- select 列1,列2,... from 表名;
select name, gender from students;
-- 使用 as 给字段起别名
-- select 字段 as 名字.... from 表名;
select name as 姓名, gender as 性别 from students;
-- 可以通过 as 给表起别名
-- select 数据表别名.字段 .... from 表名 as 别名;
select name, gender from students as s;
select students.name, students.gender from students;
-- 失败select students.name, students.gender from students as s;
select s.name, s.gender from students as s;
-- 消除重复行
-- distinct 字段
select gender from students;
select distinct gender from students;
-- 条件查询
-- 比较运算符
-- >
-- 查询大于18岁的信息
select * from students where age>18;
-- <
-- 查询小于18岁的信息
-- >=
-- <=
-- 查询小于或者等于18岁的信息
select * from students where age<=18;
-- =
-- != 或者 <>
-- 逻辑运算符
-- and
-- 18岁以上的女性
select * from students where age>18 and gender=2;
-- or
-- 18以上或者身高查过180(包含)以上
select * from students where age>18 or height>=180;
-- not
-- 不在 18岁以上的女性 这个范围内的信息
select * from students where age>18 and gender=2;
select * from students where not (age>18 and gender=2);
-- 模糊查询
-- like
-- %
-- 查询姓名中 以 "小" 开始的名字
select name from students where name like "小%";
-- _
-- 查询有2个字的名字
select name from students where name like "__";
-- 查询有3个字的名字
select name from students where name like "___";
-- rlike 正则
-- 查询以 周开始的姓名
select name from students where name rlike "^周";
select name from students where name rlike "^周.*伦$";
-- 范围查询
-- in表示在一个非连续的范围内
-- 查询 年龄为18、34的姓名
select name from students where age in (18, 34);
select name, age from students where age in (18, 34);
-- not in 不非连续的范围之内
-- 年龄不是 18、34岁之间的信息
select name, age from students where age not in (18, 34);
-- between ... and ...表示在一个连续的范围内
-- 查询 年龄在18到34之间的的信息
select * from students where age between 18 and 34;
-- not between ... and ...表示不在一个连续的范围内
-- 查询 年龄不在在18到34之间的的信息
select * from students where age not between 18 and 34;
-- 失败select * from students where age not (between 18 and 34);
-- 空判断
-- 判空is null
-- 查询身高为空的信息
select * from students where height is null;
-- 判非空is not null
select * from students where height is not null;
-- 排序
-- order by 字段
-- asc从小到大排列,即升序
-- 查询年龄在18到34岁之间的男性,按照年龄从小到到排序
select * from students where (age between 18 and 34) and gender=1 order by age; # 优先使用
select * from students where (age between 18 and 34) and gender=1 order by age asc;
-- desc从大到小排序,即降序
-- 查询年龄在18到34岁之间的女性,身高从高到矮排序
select * from students where (age between 18 and 34) and gender=2 order by height desc;
-- order by 多个字段
-- 查询年龄在18到34岁之间的女性,身高从高到矮排序, 如果身高相同的情况下按照年龄从小到大排序
select * from students where (age between 18 and 34) and gender=2 order by height desc, age asc;
-- 查询年龄在18到34岁之间的女性,身高从高到矮排序, 如果身高相同的情况下按照年龄从小到大排序,
-- 如果年龄也相同那么按照id动大到小排序
select * from students where (age between 18 and 34) and gender=2 order by height desc, age asc, id desc;
-- 按照年龄从小到大、身高从高到矮的排序
select * from students order by age asc, height desc;
-- 聚合函数
-- 总数
-- count
-- 查询男性有多少人,女性有多少人
select count(*) as 男生的人数 from students where gender=1;
select count(*) as 女生的人数 from students where gender=2;
select count(name) as 女生的人数 from students where gender=2;
-- 最大值
-- max
-- 查询最大的年龄
select max(age) from students;
-- 查询女性的最高 身高
select max(height) from students where gender=2;
-- 最小值
-- min
-- 求和
-- sum
-- 计算所有人的年龄总和
select sum(age) from students;
-- 平均值
-- avg
-- 计算平均年龄
select avg(age) from students;
-- 计算平均年龄 sum(age)/count(*)
select sum(age) / count(*) from students;
-- 四舍五入 round(123.23 , 1) 保留1位小数
-- 计算所有人的平均年龄,保留2位小数
select round(avg(age), 2) from students;
-- 计算男性的平均身高 保留2位小数
select round(avg(age), 2) from students where gender=1;
-- 分组
-- group by
-- 按照性别分组
--失败select * from students group by gender;
select gender from students group by gender;
select name from students where gender=1;
-- 计算女性的人数
select count(*) as 女性的人数 from students where gender=2 group by gender;
-- 计算男性的人数
select count(*) as 男性的人数 from students where gender=1;
-- 查询所有的性别
select gender from students group by gender;
select distinct gender from students; #
去重
-- group_concat(...)
-- 查询同种性别中的姓名
select gender, group_concat(name) from students group by gender;
select gender, group_concat(name, age) from students group by gender;
-- 查询同种性别中的人数count(*)
select gender, count(*) from students group by gender;
-- having
-- 查询每种性别中的平均年龄avg(age)
select gender, avg(age) from students group by gender;
-- 查询平均年龄超过30岁的性别,以及姓名 having avg(age) > 30
select gender, group_concat(name) from students group by gender having avg(age)>30;
-- 分页
-- limit start, count
-- 查询前5个数据
select * from students limit 0,5;
-- 查询id4-8(包含)的书序
select * from students limit 3,5;
-- 每页显示2个,第1个页面
select * from students limit 0,2;----->每页的数量*(页数-1), 每页的数量
-- 每页显示2个,第2个页面
select * from students limit 2,2;
-- 每页显示2个,第3个页面
select * from students limit 4,2;
-- 每页显示2个,显示第4页的信息, 按照年龄从小到大排序
-- 失败select * from students limit 2*(4-1),2;
select * from students limit 6,2;
-- 失败select * from students limit 6,2 order by age asc;
select * from students order by age asc limit 6,2;
--
链接查询
-- inner join ... on
-- 查询 有能够对应班级的学生以及班级信息
select * from students inner join classes on students.cls_id=classes.id;
-- 按照要求显示字典
select students.name from students inner join classes on students.cls_id=classes.id;
-- 给数据表起名字
select s.name from students as s inner join classes as c on s.cls_id=c.id;
select s.*,c.name from students as s inner join classes as c on s.cls_id=c.id;
-- 在以上的查询中,将班级姓名显示在第1列
select c.name, s.* from students as s inner join classes as c on s.cls_id=c.id;
-- 查询 有能够对应班级的学生以及班级信息, 按照班级进行排序
-- select c.xxx s.xxx from student as s inner join clssses as c on .... order by ....;
select c.name, s.* from students s
inner join classes c
on s.cls_id=c.id order by c.name;
-- left join
-- 查询每位学生对应的班级信息
select s.*, c.name from students as s left join classes as c on s.cls_id=c.id;
-- 查询没有对应班级信息的学生
-- select ... from xxx as s left join xxx as c on..... where .....
-- select ... from xxx as s left join xxx as c on..... having .....
select s.*, c.name from students as s left join classes as c on s.cls_id=c.id where c.name is null;
select s.*, c.name from students as s left join classes as c on s.cls_id=c.id having c.name is null;
-- right join
-- 将数据表名字互换位置,用left join完成
-- 自关联
-- 查询所有省份
select * from areas where pid is null;
-- 查询出山东省有哪些市
select * from areas as city inner join areas as province on city.pid=province.aid where province.atitle="山东省";
-- 查询出青岛市有哪些县城
select * from areas as city inner join areas as province on city.pid=province.aid where province.atitle="青岛市";
-- 子查询
-- 标量子查询
-- 查询出高于平均身高的信息
-- 失败select * from students height > avg(height);
-- 失败select * from students where height > select avg(height) from students;
select * from students where height > (select avg(height) from students);
-- 列子查询
-- 查询学生的班级号能够对应的学生信息
select * from students where cls_id in (1, 2);
select * from students where cls_id in (select id from classes);
子查询分类
- 标量子查询: 子查询返回的结果是一个数据(一行一列)
- 列子查询: 返回的结果是一列(一列多行)
- 行子查询: 返回的结果是一行(一行多列)