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;

  • 相关阅读:
    高级映射之事务
    配置tomcat-users.xml文件
    动态SQL之标签
    性能测试
    Service
    添加 aar 或 jar 包依赖 的方式
    安卓设备 以太网代理 问题排查
    剑指offer:面试题15、链表中倒数第 K 个结点
    剑指offer:面试题14、调整数组顺序使奇数位于偶数前面
    剑指offer:面试题13、在O(1)时间删除链表结点
  • 原文地址:https://www.cnblogs.com/mariobear/p/9180489.html
Copyright © 2011-2022 走看看