mysql 查询

# 查询
  # select * from 表名

# 消除重复行
  # 在select后面列前使用distinct可以消除重复行
    # select distinct gender from students;


  # select * from 表名 where
  # 比较运算符
    # 等于 =
    # 大于 >
    # 大于等于 >=
    # 小于 <
    # 小于等于 <=
    # 不等于 !=或<>

      # 查询科目不大于4的科目
        # select * from subjects where id<=4

      # 查询姓名不是黄蓉的学生
        # select * from students where name!='黄蓉'

      # 查询没有被删除的姓名
        # select * from students where isDelete=0;

    # 逻辑运算符
    # and
    # or
    # not

      # 查询大于三的女同学
        # select * from students where id>3 and gender=0;

      # 查询编号小于4或没有被删除的同学
        # select * from students where id<4 or isDelete=0;

    # 模糊查询
    # like
    # %表示任意多个字符
    # _表示一个任意字符

      # 查询姓黄的学生
        # select * from students where name like '黄%';
        # select * from students where name like '黄_';
      # 查询姓黄的或者带靖的学生
        # select * from students where name like '黄%' or name like '%靖%';

    # 范围查询
    # in表示在一个非连续的范围内

      # 查询编号是1或者3或者8的学生
        # select * from students where id in(1,3,8);

      # 查询编号3到8的学生
        # select * from students where id between 3 and 8;

      # 查询编号是3到8的男生
        # select * from students where id between 3 and 8 and gender=1;


    # 空判断
    # null与''是不同的
    # 判空 is null
    # 判断非空 is not null

      # 生日是空的
        # select * from students where birthday is null;

      # 生日不为是空的女生
        # select * from students where birthday is not null and gender=0;


  # 优先级
    # 小括号,not 比较运算符,逻辑运算符
    # and比or先运算,如果同时出现并希望先算or,需要结合()使用

 

# 聚合
  # 为了快速得到统计数据,提供5个聚合函数

  # count(*)表示计算总行数
    # 查询学生总数
      # select count(*) from students where isDelete=0;

  # max(列)表示求此列的最大值
    # 查询女生的编号最大值
      # select max(id) from students where gender=0;

  # min(列)表示求此列的最小值
    # 查询未删除编号最小值
      # select min(id) from students where isDelete=0;
      # select * from students where id=(select min(id) from students where isDelete=0);

  # sum(列)表示求此列的和
    # 查询男生的和
      # select sum(id) from students where gender=1;


  # avg(列)表示求此列的平均值
    # 查询未删除女生的平均值
      # select avg(id) from students where isDelete=0;


# 分组
  # 按照字段分组,表示此字段相同的数据会被放到一个数组中
    # select 列1,列2,聚合 from group by 列1,列2
      # 查询男女生总数
        # select gender as 性别,count(*) from students group by gender;

  # 数据筛选
    # having... 对分组后结果集筛选。
      # 查询男生总数
        # select gender,count(*) as re from students group by gender having gender=0;

      # 对比where与having
        # where是对from后面指定的表进行数据筛选,属于对原始集数据的筛选
        # having是对group by的结果进行筛选。


# 排序
  # 为了方便产看数据,可以对数据进行排序
  # 语法:
    # select * from 表名
    # order by列1 asc(有小到大)|desc(由大到小),列2 asc(有小到大)|desc
    # 默认按照列值由小到大

      # 查询未删除的男生由大到小
        # select * from students where isDelete=0 and gender=1 order by id desc;
      # 查询未删除科目信息,按名称升序
        # select * from subjects where isDelete=0 order by id asc;


# 分页
  # 当数据量过大时,在一页中查看数据是一件麻烦的事情
  # select * from 表名
  # limit start,count

  # 从start开始,获取count条数据
  # start索引从0开始

    # 已知:每页显示m条数据,当前显示第几页
    # 求总页数,此逻辑会在后面的python中实现、
    # 查询总条数p1
    # 使用p1除以m达到p2
    # 如果整除则p2为总数页
    # 如果不整除则p2+1为总页数

    # limit start,m

    # m = 5

    # n start
    # 1 0
    # 2 m
    # 3 2m
    # 4 3m
    # n (n-1)m

      # select * from students where isDelete=0 limit (n-1)*m,m

  # 执行顺序
    # from 表名
    # where...
    # group by...
    # select distinct *
    # having...
    # order by...
    # limit start,count

 

完整的select语句

  select distinct * 

  from  表名

  where ......

  group by ... having...

  order by ...

  limit start,count

 

# 实体与实体之间有3种对应关系,这些关系也需要存储下来
  # 关系
    # 1.试图用于完后曾查询语句的封装
    # 2.事务可以保证复杂的增删改查操作有效
    # 3.当数据巨大时,为了提高查询速度可以通过索引实现


# 创建scores,结构如下:
  # id
  # score
  # 学生
  # 科目
  # create table scores(
    # id int primary key auto_increment not null,
    # score decimal(5,2),
    # stuid int,
    # subid int,
    # foreign key(stuid) references students(id),
    # foreign key(subid) references subjects(id)
  # )


  # 外键的级联操作

    # 级联操作的类型包括:
      # restrict(限制):默认值,抛异常
      # cascade(级联):如果主表的记录删掉了,则从表种相关的数据都将被删除
      # set null: 将外键设置为空
      # no action: 什么都不做

  # 连接
    # 链接查询
      # 查询学生郭靖python科目的成绩
        # 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 students.name,subjects.title,scores.score from students inner join scores on scores.stuid=students.id inner join subjects on scores.subid=subjects.id;


    # 链接查询3种
      # 表A inner join 表b; 表A与表B匹配的
      # 表A left join 表b; 以左表的信息为准,外加表A中独有的数据,未对应的数据使用null填充
      # 表A right join 表b; 以右表的信息为准,外加表B中独有的数据,未对应的数据使用null填充

      # select name,avg(score) from scores
      # inner join students on scores.stuid=students.id
      # group by stuid;

 

# select distinct 列*
# from inner|left|right join 表2 on 表1与表2的关系
# where 比较运算符(>,>=,<,<=,=,!=,<>),逻辑运算符(and,or,not),空判断,模糊查询(like)
# group by ... (聚合) 属性 having 属性=值.
# order by ... (排序) asc | desc
# limit start,count

 

posted @ 2018-08-31 14:17  红尘陌上,独自行走  阅读(137)  评论(0编辑  收藏  举报