标签:连续 height ... varchar like word 集合 char opera
数据库设计规范
-- 创建数据库
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),
gender 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, "python_01期"), (0, "python_02期");
select * from 表名;
例:
select * from students;
select 列1,列2,... from 表名;
例:
select name from students;
select id as 序号, name as 名字, gender as 性别 from students;
-- 如果是单表查询 可以省略表明
select id, name, gender from students;
-- 表名.字段名
select students.id,students.name,students.gender from students;
-- 可以通过 as 给表起别名
select s.id,s.name,s.gender from students as s;
select distinct 列1,... from 表名;
例:
select distinct gender from students;
使用where子句对表中的数据筛选,结果为true的行会出现在结果集中
select * from 表名 where 条件;
例:
select * from students where id=1;
例1:查询编号大于3的学生
select * from students where id > 3;
例2:查询编号不大于4的学生
select * from students where id <= 4;
例3:查询姓名不是“黄蓉”的学生
select * from students where name != ‘黄蓉‘;
例4:查询没被删除的学生
select * from students where is_delete=0;
例5:查询编号大于3的女同学
select * from students where id > 3 and gender=0;
例6:查询编号小于4或没被删除的学生
select * from students where id < 4 or is_delete=0;
例7:查询姓黄的学生
select * from students where name like ‘黄%‘;
例8:查询姓黄并且“名”是一个字的学生
select * from students where name like ‘黄_‘;
例9:查询姓黄或叫靖的学生
select * from students where name like ‘黄%‘ or name like ‘%靖‘;
例10:查询编号是1或3或8的学生
select * from students where id in(1,3,8);
例11:查询编号为3至8的学生
select * from students where id between 3 and 8;
例12:查询编号是3至8的男生
select * from students where (id between 3 and 8) and gender=1;
例13:查询没有填写身高的学生
select * from students where height is null;
例14:查询填写了身高的学生
select * from students where height is not null;
例15:查询填写了身高的男生
select * from students where height is not null and gender=1;
为了方便查看数据,可以对数据进行排序
select * from 表名 order by 列1 asc|desc [,列2 asc|desc,...]
例1:查询未删除男生信息,按学号降序
select * from students where gender=1 and is_delete=0 order by id desc;
例2:查询未删除学生信息,按名称升序
select * from students where is_delete=0 order by name;
例3:显示所有的学生信息,先按照年龄从大-->小排序,当年龄相同时 按照身高从高-->矮排序
select * from students order by age desc,height desc;
为了快速得到统计数据,经常会用到如下5个聚合函数
例1:查询学生总数
select count(*) from students;
例2:查询女生的编号最大值
select max(id) from students where gender=2;
例3:查询未删除的学生最小编号
select min(id) from students where is_delete=0;
例4:查询男生的总年龄
select sum(age) from students where gender=1;
-- 平均年龄
select sum(age)/count(*) from students where gender=1;
例5:查询未删除女生的编号平均值
select avg(id) from students where is_delete=0 and gender=2;
值的集合
做一些操作当数据量过大时,在一页中查看数据是一件非常麻烦的事情
select * from 表名 limit start,count
例1:查询前3行男生信息
select * from students where gender=1 limit 0,3;
select * from students where is_delete=0 limit (n-1)*m,m
当查询结果的列来源于多张表时,需要将多张表连接成一个大的数据集,再选择合适的列返回
mysql支持三种类型的连接查询,分别为:
内连接查询:查询的结果为两个表匹配到的数据
右连接查询:查询的结果为两个表匹配到的数据,右表特有的数据,对于左表中不存在的数据使用null填充
select * from 表1 inner或left或right join 表2 on 表1.列 = 表2.列
例1:使用内连接查询班级表与学生表
select * from students inner join classes on students.cls_id = classes.id;
例2:使用左连接查询班级表与学生表
select * from students as s left join classes as c on s.cls_id = c.id;
例3:使用右连接查询班级表与学生表
select * from students as s right join classes as c on s.cls_id = c.id;
例4:查询学生姓名及班级名称
select s.name,c.name from students as s inner join classes as c on s.cls_id = c.id;
设计市信息的表结构citys
citys表的proid表示城市所属的省,对应着provinces表的id值
能不能将两个表合成一张表呢?
观察两张表发现,citys表比provinces表多一个列proid,其它列的类型都是一样的
存储的都是地区信息,而且每种信息的数据量有限,没必要增加一个新表,或者将来还要存储区、乡镇信息,都增加新表的开销太大
定义表areas,结构如下
- id
- atitle
- pid
create table areas(
aid int primary key,
atitle varchar(20),
pid int
);
source areas.sql;
select count(*) from areas where pid is null;
select city.* from areas as city
inner join areas as province on city.pid=province.aid
where province.atitle=‘山西省‘;
select dis.* from areas as dis
inner join areas as city on city.aid=dis.pid
where city.atitle=‘广州市‘;
在一个 select 语句中,嵌入了另外一个 select 语句, 那么被嵌入的 select 语句称之为子查询语句
主要查询的对象,第一条 select 语句
查询班级学生的平均身高
select * from students where age > (select avg(age) from students);
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);
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
标签:连续 height ... varchar like word 集合 char opera
原文地址:https://www.cnblogs.com/yinjiangchong/p/9404349.html