3 数据库基础

分组与筛选
  • 分组
    • select count(*) from tbname group by 需要分组字段
      (显示tbname表中需要分组字段的数量)
    • group_concat(字段名)
      (详细显示字段名具体数据)
    • group by 需要分组字段 with rollup
      (分组之后的总聚合显示)
    • group by 需要分组字段 having 筛选条件
      (分组后的筛选)
    • select gender,count(*),group_concat(name,":",age) from students group by gender having count(*)>2;
      (示例)
  • 分组后的筛选
    • 原始数据集–>(where)–>结果集–>(group by)–>结果集–>(having)–>结果集
排序
  • order by 字段
    (默认升序)
  • order by 字段 asc
    (指定升序)
  • order by 字段 desc
    (指定降序)
  • select * from studnets where (age between 18 and 26) and gender="男" order by age asc,high desc;
    (示例)
限制
  • limit start,count
    (start为偏移量,默认为0,count为条数)
  • select * from students limit 0,3
    select * from students limit 3,3
    (制作分页示例)
  • 注意
    • limit 不能写数学公式
    • limit 只能写在末尾
表连接
  • 当查询结果的列来源于多张表时,需要将多张表连接成一个大的数据集,再选择合适的列返回mysql,这时我们需要使用表连接
  • 内连接
    • 选出2张表中互相匹配的记录
  • 外连接
    • 左连接
      • 查询结果为2个表匹配到的数据,左表持有的数据,对于右表中不存在的数据使用null填充
    • 右连接
  • select * from tb1 inner|left|right join tb2 on tb1.col=tb2.col;
子查询
  • 某些情况下,当进行查询时,需要的条件是另一个selet语句的结果,这时就要用到子查询
  • select * from tb1 where hight=(select max(hight) from tb1);
  • select * from students gender=2 and age=(select max(age) from students where gender=2);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值