• 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;

  • 相关阅读:
    android 联系数据库
    shuffle一个简单的过程叙述性说明
    每天的学习经验:SharePoint 2013 定义自己添加的产品清单。Callout菜单项、文档关注、SharePoint服务机端对象模型查询
    函数调用
    InputMonitor注意事项
    处理FTP上传成功推理
    用Web技术开发客户端(一)
    简析Chrome和Webkit的渊源
    历史在重演:从KHTML到WebKit,再到Blink
    开发者应当了解的WebKit知识
  • 原文地址:https://www.cnblogs.com/mariobear/p/9180489.html
Copyright © 2020-2023  润新知