SQL查询操作

SQL查询操作

普通查询

  • 创建数据库
create database python_test_1 charset=utf8;
  • 使用数据库
use python_test_1;
  • 显示当前使用的数据库
select database();
  • 创建一个数据表
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_deleted bit default 0
    );

create table classes(
    id int unsigned auto_increment primary key,
    name varchar(20) default ''
    );
  • 向数据表添加数据
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);
  • 查询所有字段
select * from students;
select * from classes;
select id,name from classes;
  • 查询指定字段
select name,age from students;
  • 给字段起别名,
  • 字段 as 别名
select name as 姓名, age as 年龄 from students;
  • 给表起别名
  • 表名 as 别名
select students.name, students.age from students as stu;
select stu.age, stu.name from students as stu;
  • 消除重复行
  • distinct 字段
select distinct gender from students;

条件查询

比较运算符

  • 大于, >
select * students where age>18;
select id,name,gender students where age>18;
  • 小于, <
select * students where age<18;
  • 等于, = (SQL无赋值)
select * students where age=18;
  • 不等于, !=
select * students where age!=18;

逻辑运算符

  • 与, and
select * from students where age>18 and age<28
select * from students where age>18 and gender='女';
  • 或, or
select * from students where age>18 or height>=180;
  • 非, not
select * from students where age>18 not age=28;
select * from students where not (age>18 and gender=2);
select * from students where (not age<=18) and gender=2;

模糊查询

  • like
  • % 替换1个或者多个
  • _ 替换1个
-- 查询姓名中以"小"开始的名字
select name from students where name like '小%';
-- 查询姓名中有"小"的所有名字
select name from students where name like "%小%";
-- 查询有2个字的名字
select name from students where name like "__";
-- 查询有3个字的名字
select name from students where name like "___";
-- 查询至少有2个字以上的名字
select name from students where name like "__%";
  • rlike 正则表达式
-- 查询以'周'开始的姓名
select name from students where name rlike '^周.*';
-- 查询以'周'开始,'伦'结尾的姓名
select name from students where name rlike "^周.*伦$";

范围查询

  • in 表示在一个非连续的的范围内查询
-- 查询年龄为12,18,34的数据
select name, age from students where age in (12, 18, 34);
  • not in 不在非连续的范围之内
select name, age from students where age not in (12, 18, 34);
  • between … and … 表示在一个连续的范围内
select name, age from students where age between 18 and 34;
  • not between … and … 表示不再一个连续的范围内
-- 两句查询结果相同,逻辑不同
select name, age from students where age not between 18 and 34;
select name, age from students where not age 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 升序
  • desc 降序
  • 默认按主键升序排列
-- 年龄18到30岁之间的男性,按照年龄升序排列
select * from students where (age between 18 and 30) and gender=1;
select * from students where (age between 18 and 30) and gender=1 order by age;
-- 年龄在18到34之间的女性,按照身高降序排列
select * from students where (age between 18 and 34) and gender=2;
select * from students where (age between 18 and 34) and gender=2 order by height desc;
-- 年龄在18到34之间,按身高升序排,按年龄升序排,按id降序排(如果有数据相同)
select * from students where (age between 18 and 34) and gender=2 order by height asc,age asc,id desc;
-- 按年龄从小到大,身高从高到矮排列
select * from students order by age asc, height desc; 

聚合函数

  • 总数, count
-- 查询男性有多少人
select * from students where gender=1;
select count(*) from students where gender=1;
select count(*) as 男性人数 from students where gender=1;
  • 最大值, max
-- 查询最大的年龄
select age from students;
select max(age) from students;
-- 查询女性的最高身高
select max(height) from students where gender=2;
  • 最小值, min
select min(height) from students where gender=2;
  • 求和, sum
-- 计算所有人的年龄总和
select sum(age) from students;
  • 平均值, avg
select avg(age) from students;
select sum(age)/count(*) from students;
  • 四舍五入, round(…,1) 保留1位小数
  • 为了避免误差,不建议存储小数(计算机存储近似值)
-- 计算所有人的平均年龄,保留两位小数
select round(sum(age)/count(*),2) from students;
select round(sum(age)/count(*),3) from students;
-- 计算男性的平均身高,保留2位小数
select round(avg(height),2) from students where gender=1;

分组(与聚合函数搭配使用)

  • group by … 以…分组
-- 按照性别分组,查询所有的性别
select gender from students group by gender;
select gender, count(*) students group by gender;
-- 计算男性的人数
select gender, count(*) from students where gender=1 group by gender;
  • group_concat(…) 显示组内包含的信息
select gender, group_concat(name) from students where gender=1 group by gender;
select gender, group_concat(name, age, id) from students where gender=1 group by gender;
select gender, group_concat(name,'_', age, '-', id) from students where gender=1 group by gender;
  • having, 判断分组(where对表约束,having对分组约束)
-- 查询平均年龄大于30岁的性别以及姓名
select gender, group_concat(name) from students group by gender having avg(age)>30;
-- 查询每中性别中的人数多于2个的信息
select gender, group_concat(name) from students group by gender having count(*)>2;

分页

  • limit start, count (从start开始,最小为0,count为个数)
-- 限制查询个数
select * from students where gender=1 limit 2;
-- 查询前五个信息
select * from students limit 0, 5;
-- 查询id为6到10(包含)的信息
select * from students limit 5, 5;
-- 每页显示2个,第1个页面
select * from students limit 0,2;
-- 每页显示2个,第2个页面
select * from students limit 2,2;
-- 每页显示2个,第3个页面
select * from students limit 4,2;
-- 每页显示2个,第4个页面
select * from students limit 6,2;
-- 规律: limit (第N页-1)*每页显示个数, 每页显示个数
-- 每页显示2个,显示第6页的信息,按照年龄从大到小排序
select * from students order by age asc limit 10,2;
select * from studnets where gender=2 order by height desc limit 0,2;

连接查询

  • 内连接(两张表的交集)
  • inner join … on 条件
-- select * from 表A inner join 表B;
select * from students inner join classes;
-- 查询能够对应班级的学生以及班级信息
select * from students inner join classes on students.cls_id=classes.id;
-- 按要求显示姓名,班级
select students.*, classes.name from students inner join classes on students.cls_id=classes.id;
select students.name, classes.name from students inner join classes on students.cls_id=classes.id;
-- 给数据表起名
select s.name, c.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;
-- 查询又能够对应班级的学生以及班级信息,显示学生的所有信息,只显示班级的名称,以班级名称和学生id排序
select s.*, c.name from students as s inner join classes as c on s.cls_id=c.id  order by c.name,s.id;
  • 左连接
  • left join … on 条件
  • 以left join左边的表为基准显示,对于右边的表没有的数据以null填充
-- 查询每位学生对应的班级信息
select * from students as s left join classes as c on s.cls_id=c.id;
-- 查询没有对应班级信息的学生(where从原表约束,having从结果约束)
select * from students as s left join classes as c on s.cls_id=c.id having c.id is null;
select * from students as s left join classes as c on s.cls_id=c.id where c.id is null;
  • 右连接
  • right join … on 条件 (与左连接互换表的位置,一般不用)
select * from students as s right join classes as c on s.cls_id=c.id  order by c.name,s.id;

自关联

  • 分析数据表结构


    • 设计省份信息表的结构provinces

    • id
    • ptitle
    • 设计城市信息表的结构city
    • id
    • ctitle
    • pid
    • city表的pid表示城市所属的省份,对应provinces表的值
    • 合并成一张表后
    • 定义表areas
    • aid
    • atitle
    • pid
    • 说明:

  • 因为省份没有所属的上级地区,所以可以填写为null
  • 城市所属的省份pid,填写省份所对应的编号id
  • 这就是自关联,表中的某一列,关联了这个表中的另外一列,但是它们的业务逻辑含义是不一样的,城市信息的pid引用的是省份信息的id
  • 在这个表中,结构不变,可以添加区县、乡镇街道、村社区等信息
  • 创建数据库

  • create table areas(
        aid int primary key,
        atitle varchar(30),
        pid int
        );
    • 准备数据(不完全完整和准确): 省级联动
    • 导入areas.sql文件
    # 命令行下进入areas.sql文件所在路径,登录mysql选择数据库
    source areas.sql;
    • 查询所有省份
    select * from areas where pid is null;
    • 查询某个省份包含哪些城市
    -- 利用重命名来实现自关联
    select * from areas as province inner join areas as city on city.pid=province.aid having province.atitle="湖北省";
    select province.atitle, city.atitle from areas as province inner join areas as city on city.pid=province.aid having province.atitle="湖北省";
    • 查询某个城市包含哪些地区
    select province.atitle, city.atitle from areas as province inner join areas as city on city.pid=province.aid having province.atitle="武汉市";
    select * from areas where pid=(select aid from areas where atitle="武汉市");

    子查询

    • select语句嵌套select语句
    • 标量子查询
    -- 查询最高的男生信息
    select * from students where height = 188;
    select name from classes where id in (select cls_id from students);
    select * from areas where pid = (select aid from areas where atitle="湖北省");
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值