创建数据库、数据表
– 创建数据库
create database python_test_1 charset=utf8;
– 使用数据库
use python_test_1;
– students表
create table students(
-> id int unsigned primary key auto_increment not null,
-> name varchar(20) default “”,
-> age tinyint unsigned default 0,
-> height decimal(5,2),
-> gendgegegender enum(‘男’,‘女’,‘中性’,‘保密’) default ‘保密’,
-> cls_id int unsigned default 0,
-> is_delete bit default 0
-> );
– classes表
create table classes (
id int unsigned auto_increment primary key not null,
name varchar(30) not null
);
准备数据
– 向students表中插入数据
insert into students values
(0,‘小明’,18,180.00,2,1,0),
(0,‘小月月’,18,180.00,2,2,1),
(0,‘彭于晏’,29,185.00,1,1,0),
(0,‘刘德华’,59,175.00,1,2,1),
(0,‘黄蓉’,38,160.00,2,1,0),
(0,‘凤姐’,28,150.00,4,2,1),
(0,‘王祖贤’,18,172.00,2,1,1),
(0,‘周杰伦’,36,NULL,1,1,0),
(0,‘程坤’,27,181.00,1,2,0),
(0,‘刘亦菲’,25,166.00,2,2,0),
(0,‘金星’,33,162.00,3,3,1),
(0,‘静香’,12,180.00,2,4,0),
(0,‘郭靖’,12,170.00,1,4,0),
(0,‘周杰’,34,176.00,2,5,0);
– 向classes表中插入数据
insert into classes values (0, “1班”), (0, “2班”);
查询所有字段
select * from 表明;
查找指定字段
select id, name from students;
给指定字段起别名
select id as ‘编号’ from students;
给指定列名起别名
select stu.id as ‘编号’ from students as stu;
消除重复行
select distinct id from students;
条件语句
优先级由高到低的顺序为:小括号,not,比较运算符,逻辑运算符
and比or先运算,如果同时出现并希望先算or,需要结合()使用
例如:
select * from students where id >3 and id < 10 or height is null;
比较运算符
等于: =
大于: >
大于等于: >=
小于: <
小于等于: <=
不等于: != 或 <>
例句:
select * from students where id > 3;
#逻辑运算符
and
or
not
例句:
select * from students where id > 3 and gender = ‘男’;
#模糊查询
like
%表示任意多个任意字符
_表示一个任意字符
例如:
select * from students where name like ‘王%’;
#范围查询
in表示在一个非连续的范围内
例如:
select * from students where id in(1, 15);
#空判断
注意:null与’'是不同的
判空is null
例如:
select * from students where height is null;
#排序:
order by
将行数据按照列1进行排序,如果某些行列1的值相同时,则按照列2排序,以此类推
默认按照列值从小到大排列(asc)
asc从小到大排列,即升序
desc从大到小排序,即降序
select * from students where gender=1 order by age desc;
#聚合函数:
count()表示计算总行数,括号中写星与列名,结果是相同的
例如:
mysql> select count() from students;
#最小值:
例如:
select min(id) from students where is_delete=0;
#最大值:
例如:
select max(id) from students where is_delete=0;
#求和:
例如:
select sum(age) from students where gender=1;
#平均值:
例如:
select avg(id) from students where is_delete=0 and gender=2;
#分组:
例如:
select gender from students group by gender;
group_concat(字段名)可以作为一个输出字段来使用,
表示分组之后,根据分组结果,使用group_concat()来放置每一组的某字段的值的集合
例如:
select gender,group_concat(name) as ‘姓名’ from students group by gender;
#连接查询
内连接查询:查询的结果为两个表匹配到的数据
右连接查询:查询的结果为两个表匹配到的数据,右表特有的数据,对于左表中不存在的数据使用null填充
左连接查询:查询的结果为两个表匹配到的数据,左表特有的数据,对于右表中不存在的数据使用null填充
select * from 表1 inner或left或right join 表2 on 表1.列 = 表2.列
例如 :
mysql> select * from students where cls_id in (select id from classes);
#自关联
自己内连接自己
例如:
略
#子查询
在一个 select 语句中,嵌入了另外一个 select 语句, 那么被嵌入的 select 语句称之为子查询语句
#主查询
主要查询的对象,第一条 select 语句
#主查询和子查询的关系
子查询是嵌入到主查询中
子查询是辅助主查询的,要么充当条件,要么充当数据源
子查询是可以独立存在的语句,是一条完整的 select 语句
#子查询分类
标量子查询: 子查询返回的结果是一个数据(一行一列)
列子查询: 返回的结果是一列(一列多行)
行子查询: 返回的结果是一行(一行多列)
#标量子查询
查询班级学生平均年龄
查询大于平均年龄的学生
查询班级学生的平均身高
select * from students where age > (select avg(age) from students);
#列级子查询
查询还有学生在班的所有班级名字
找出学生表中所有的班级 id
找出班级表中对应的名字
select name from classes where id in (select cls_id from students);
#行级子查询
需求: 查找班级年龄最大,身高最高的学生
行元素: 将多个字段合成一个行元素,在行级子查询中会使用到行元素
select * from students where (height,age) = (select max(height),max(age) from students);
#子查询中特定关键字使用
in 范围
格式: 主查询 where 条件 in (列子查询)
#总结:
完整的select语句
非人语版本:
SELECT select_expr [,select_expr,…] [
FROM tb_name
[WHERE 条件判断]
[GROUP BY {col_name | postion} [ASC | DESC], …]
[HAVING WHERE 条件判断]
[ORDER BY {col_name|expr|postion} [ASC | DESC], …]
[ LIMIT {[offset,]rowcount | row_count OFFSET offset}]
]
讲人话!:
select distinct *
from 表名
where …
group by … having …
order by …
limit start,count
执行顺序为:
from 表名
where …
group by …
select distinct *
having …
order by …
limit start,count