zoukankan      html  css  js  c++  java
  • MySql单表查询

    1.查询所有记录
    select  *    from department;
    select  *   from student;
    select  *   from student_detail;

    2.查询选中列记录

    3.查询指定条件下的记录
    select   s_name   from student where s_id>2;

    4.查询后为列取别名
    select  s_name as 姓名   from    student;

    5.模糊查询

    insert into student(s_name,dept_id ) values('张三丰',2),('小明',3),('小红',2);
     select * from student where s_name like '小%';

    6.排序ORDER BY     :  ASC升序(默认)      DESC降序
    降序:   select  *  from  `select`  order by `s_id` desc;

    7.限制显示数据的数量LIMIT
    #按学生学号升序输出的前4条数据
    mysql> select * from `select` order by s_id limit 2;

    #指定的返回的数据的位置和数量
    mysql> select * from `select` order by s_id limit  2,2;


    8.常用聚合函数
     #求最大年龄  
    mysql> select  MAX(age)   from  student_detail;

    #求最小年龄
    mysql> select  MIN(age) from student_detail;

    #求和
    mysql> select  SUM(age)  from  student_detail;

    #求平均数
    mysql> select  AVG(age)   from  student_detail;

    #四舍五入
    mysql> select  ROUND(AVG(age))   from   student_details;

    #统计
    mysql> select count(s_id)   from  student;

    9.分组查询GROUP BY

    #对学生表中学院栏进行分组,并统计学院的学生人数:
    mysql> select   dept_id  as 学院id,  count(dept_id)   as  学生个数 from   student  group by  dept_id;

    HAVING分组条件
    HAVING 后的字段必须是SELECT后出现过的

    # 查看 哪些学院,只有一个学生
    mysql> select  dept_id  as 学院id , count(dept_id)   as  学生个数 from   student    group  by   dept_id   having  学生个数=1;

  • 相关阅读:
    day10T3改错记
    day9T1改错记
    day8T1改错记
    洛谷P5068[Ynoi2015]我回来了(bfs+bitset)
    BZOJ4939[Ynoi2016]掉进兔子洞(莫队+bitset)
    [学习笔记]dsu on tree
    [学习笔记]FWT(快速沃尔什变换)
    [学习笔记]FMT(快速莫比乌斯变换)&子集卷积(待填坑)
    POJ-1743-Musical Theme(后缀数组)
    后缀数组模板
  • 原文地址:https://www.cnblogs.com/mariobear/p/9180489.html
Copyright © 2011-2022 走看看