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;

  • 相关阅读:
    np背包问题【算法:折半枚举】
    数字游戏【后缀积问题,一个数学分析问题】
    bfs求最短路径
    利用费马小定理求逆元
    [蓝桥杯2016初赛]剪邮票【全排列,连通块】
    np背包问题【算法:折半枚举】
    火星救[数学,一点前缀和]
    bfs求最短路径
    检测一个正整数是否是2的N次方
    IIS与asp.net身份认证
  • 原文地址:https://www.cnblogs.com/mariobear/p/9180489.html
Copyright © 2011-2022 走看看