学习日记28--MySQL学习2

          • 查询和筛选数据
          • 聚合函数
          • 分组group by…having…
          • 排序 order by…asc|desc
          • 分页 limit start,count
          • 存储关系
          • 外键连接
          • 小结

查询和筛选数据
  1. select * from 表名 where 条件;
    from关键字后面写表名,表示数据来源于是这张表
    select后面写表中的列名,如果是*表示在结果中显示表中所有列
    在select后面的列名部分,可以使用as为列起别名,这个别名出现在结果集中
    如果要查询多个列,之间使用逗号分隔
  2. 消除重复行
    在select后面列前使用distinct可以消除重复的行
    select distinct gender from students;
    注意:重复是指,整个一行所有元素相同。
  3. 比较运算符
    等于=
    大于>
    大于等于>=
    小于<
    小于等于<=
    不等于!=或<>
  4. 逻辑运算符
    and //筛选条件更严苛
    or //筛选范围变大
    not

    select * from 表名 where id>3 and gender=0;
  5. 范围查询
    in表示在一个非连续的范围内:
    select * from 表名 where id in(1,3,8);
    between … and …表示在一个连续的范围内
    select * from 表名 where id between 3 and 8; (双闭区间)
  6. 模糊查询
    like
    %表示任意多个任意字符
    _表示一个任意字符
    例:where sname like ‘黄%’ ; where sname like ‘黄_’ or sname like ‘%靖%’;(查询一个姓黄或名字有靖的学生)
  7. 空判断
    null 与 ‘’ 是不同的
    查询没有填写地址的学生
    select * from students where hometown is null;(不能用=null)
    查询填写了地址的学生
    select * from students where hometown is not null;
  8. 优先级
    小括号>not>比较运算符>逻辑运算符(not除外)(and 和or 是最低的,and又比or优先)
聚合函数
  1. count(*)计算总行数, max(列) , min(列), sum(列),avg(列)
    例子:select count(*) from 表名 where isdelete=0;
    select max(id) from students where isdelete=0 and gender=0;
    select sum(id) from 表名 where …
    select avg(id) from 表名 where …

  2. 子查询:将聚合函数与查询语句嵌套
    select * from students where id = (select min(id) from students where isdelete = 0); ,注意索引要对应

分组group by…having…
  1. 对分组后的数据进行聚合函数统计:
    select gender [as 别名] ,count(*) from 表名 group by gender;
    ★select和from之间是显示的结果集
  2. having后面的条件运算符与where的相同:
    目标:查询男生人数
    方案一
    select gender,count(*)
    from students
    where gender=1; //where对原数据集进行筛选
    方案二:
    select gender as xb,count(*)
    from students
    group by gender
    having xb=1; //having对筛选之后的数据集进行筛选(增加判断条件)
  3. select gender,count(*) as rs from students group by gender having rs>2; //因为已经有先一步结果集了,所以可以对结果集再次进行having筛选。
排序 order by…asc|desc
  1. select * from 表名
    order by 列1 asc|desc,列2 asc|desc,…
    将行数据按照列1进行排序,如果某些行列1的值相同时,则按照列2排序,以此类推
    默认按照列值从小到大排列
    asc从小到大排列,即升序:ascend
    desc从大到小排序,即降序:descend
    例:select * from students where gender=1 and isdelete=0 order by id desc; 对未删除的男生按id降序排序
分页 limit start,count
  1. select * from 表名 limit start,count
    从start开始,获取count条数据,start索引从0开始
    例子:select * from students limit 1,3; 表示从第2个数据开始取3个
  2. 已知:每页显示m条数据,当前显示第n页。求第n页的数据:
    select * from students where isdelete=0 limit (n-1)*m,m;
存储关系
  1. 若A和B两张表的关系是1对1,则关系字段存在A,B均可。
  2. 若A对B是一对多,则关系字段存在B表当中。
  3. 若A对B是多对多,则新建一张表存储关系。
  4. 关系不能闭合,否则会产生冗余
外键连接
  1. 外键约束:是两个表建立关系之后,为了保证属性值的正确的一种约束,它由外键进行约束。(先有关系,才有外键。在ER图中不存在外键,建立表时才有外键)
  2. 新建一个表格 create table scores(
    id int primary key auto_increment,
    stuid int, //与外键对应的键
    foreign key(stuid) references students(id) //建立连接
    );
  3. 在删除students表的数据时,如果这个id值在scores中已经存在,则会抛出异常。
  4. 级联操作:如果主表的记录删掉,则从表中相关的记录都将被删除。级联操作的类型有:
    1. restrict(限制):默认值,抛异常
    2. cascade(级联):如果主表的记录删掉,则从表中相关的记录都将被删除。——->(做逻辑删除即可,不要物理删除)
    3. set null:将外键设置为空
    4. no action: 什么都不做
  5. (当结果来自多张表时)连接查询:select students.name, scores.score from scores inner join students on scores.stuid = students.id; //内连接,查询表的顺序不分,等号前后可以互换

    select students.name,subjects.title,scores.score from scores 
    inner join students on scores.stuid=students.id 
    inner join subjects on scores.subid=subjects.id;
    

    select * from students left join scores on students.id = scores.stuid // 左连接,以left左侧表数据为主数据;
    <==>select * from scores right join students on scores.stuid= students.id // 右连接,以right右侧表数据为主数据;



    ★三种连接的结果集不一样!
    A inner join B:表A与表B都有的行才会出现
    A left join B:都有的出现+A独有的数据(null填充)
    A right join B:都有的出现+B独有的数据(null填充)

小结

一个完整的sql语句结构
select distinct 列*
from 表1 inner|left|right join 表2 on 表1与表2的关系
where …
group by …having …
order by… asc|dese
limit start,count

你可能感兴趣的:(日记)