zoukankan      html  css  js  c++  java
  • MySQL之关键字

    关键字:

    select * from 表名 
    	where 
    	group by
    	having
    	distinct
    	order by
    	limit a,b
    	between * and *   
    

    测试数据

    # 测试数据
    create table stu(id int primary key auto_increment,name char(10),math float,english float);
    insert into stu values(null,"赵云",90,30);
    insert into stu values(null,"小乔",90,60);
    insert into stu values(null,"小乔",90,60);
    insert into stu values(null,"大乔",10,70);
    insert into stu values(null,"李清照",100,100);
    insert into stu values(null,"铁拐李",20,55);
    insert into stu values(null,"小李子",20,55);
    
    1. where关键字
    
    # where后面可以接 运算符
    select *from stu where english > 60;
    select *from stu where english < 60;
    select *from stu where english = 60;
    select *from stu where english <= 60;
    select *from stu where english >= 60;
    
    
    # where后面可以接 in ,not in
    select *from stu where english in (60,70,80);
    select *from stu where english not in (60,70,80);
    
    
    # where后面可以接 逻辑运算符  and,or,not
    select *from stu where english >= 60 and math >=60;
    select *from stu where english >= 60 or math >=90;
    select *from stu where  not english >= 60;  # 英语小于60分的记录
    select *from stu where  not english >= 60 and math >=60; # 英语小于60 and 数学大于等于60
    select *from stu where  not (english >= 60 and math >=60); # 去掉 英语大于等于60 并且 数学大于等于60 的人。 换一句话说,只要有一门不及格就显示出来。
    
    
    # where后面可以接 模糊查询 like  % 表示 0个或多个。_表示一个任意字符
    select * from stu where name like "小%";
    
    
    # where后面可以接 BINARY 区分大小写
    select * from stu where BINARY name like "a%";	# 查询以小写字母a开头的姓名
    
    
    
    # 练习题1:查询name以小开头的名字,math < 80 并且 英语大于 20的人的数学成绩
    select math from stu where math < 80 and english >20 and name like "小%";
    
    

    2. between * and *

    select * from stu where english between 60 and 90;
    

    3. distinct 去除重复记录

    # distinct 写在 * 的前面
    select distinct *  from stu;	# 只有 所有的字段 都存在重复的,才会去重
    select distinct name  from stu; # 只要 name 重复,就会去掉重复
    

    4. group by

    测试数据
    create table emp (id int,name char(10),sex char,dept char(10),job char(10),salary double);
    
    insert into emp values
    (1,"刘备","男","市场","总监",5800),
    (2,"张飞","男","市场","员工",3000),
    (3,"关羽","男","市场","员工",4000),
    (4,"孙权","男","行政","总监",6000),
    (5,"周瑜","男","行政","员工",5000),
    (6,"小乔","女","行政","员工",4000),
    (7,"曹操","男","财务","总监",10000),
    (8,"司马懿","男","财务","员工",6000);
    
    简单练习:
    1.查询每个部门有几个人
    
    2.计算每个部门的平均工资
    
    3.计算每个岗位的平均工资
    
    4.计算每个部门每个岗位的平均工资
    
    5.查询平均工资大于5000的部门
    
    # gourp 是分组的意思,即将一个整体按照某个特征或依据来分为不同的部分
    
    # 为什么会有分组?  为了统计,例如统计男性有几个,女性有几个
    
    # 统计函数
    	# 也称为聚合函数,就是将一堆数据经过计算得出一个结果
    
    # 练习题 
    	# 求最大工资的姓名。
    	select name from emp where salary = max(salary); # 这个sql执行报错,max()需要遍历完所有的结果。where 字句 后面并没有遍历完,所以聚合函数不能放到where的后面
    	
    # 聚集函数
    	AVG()	# 求平均数
    
    	SUM()	# 求和
    
    	MAX()	# 求最大值
    
    	MIN()	# 求最小值
    	
    	COUNT() # 在查询某个字段有多少行时,null是不计入count的。
    		
    
    # 如何使用group by 
    select [字段1,...,字段n|*] from 表名 group by 字段1,...,字段n
    
    
    # 容易出现问题
    select * from emp group by dept;
    mysql5.6 下查询的结果是name仅显示该分组下的一个数据。(按理应该报错,但是在5.6之后,开启了模糊模式。我们可以改成严格模式)
    
    # 练习题
    1. 查询出部门人数小于3个人的部门名称,人员姓名,具体的人数
    mysql> select dept,group_concat(name),count(dept)as person_nums from emp group by dept having person_nums <3;
    +--------+--------------------+-----+
    | dept   | group_concat(name) | person_nums |
    +--------+--------------------+-----+
    | 财务    | 曹操,司马懿          |   2 |
    +--------+--------------------+-----+
    1 row in set (0.28 sec)
    
    
    # as 去别名,可以省略
    select name, english + math as 总分  from stu;
    select name, english + math 总分  from stu;
    
    having # 过滤
    # 作用:
    """
    	1. 用于对分组后的数据进行刷选
    	2. 作用跟where相同,用于过滤
    """
    
    # having与where的不同点
    """
    	1. 不同点在于where是从文件读取数据时的过滤条件,这导致了where中不能使用聚合函数
    	2. having是分组后进行的过滤。
    """
    
    # 为什么 不分组的时候在select 前面可以使用聚合函数呢?
    """
    	select SUM(salary) from where ...;
    	因为你where比select 后面的字段筛选更早执行 此时数据全都已经读取了 所以可以进行统计
    """
    
    # 练习题
    """
    	1. 查询 岗位平均薪资高于6000的 岗位名称和平均薪资
    		select dept,avg(salary) from emp group by dept having avg(salary) > 6000 ;
    	2. 查询 部门人数少于3的 部门名称 人员名称 人员个数
    		select dept,group_concat(name),count(*) from emp group by dept having 	count(name) < 3;
    """
    

    5. 聚合函数:

    # 用户处理字符串的函数
    concat(str1,str2,str3…)	 # 合并字符串函数
    strcmp(str1,str2)  # 比较字符串大小函数
    length(str)	 # 获取字符串字节数函数
    char_length(str) # 获取字符串字符数函数
    

    6. order by

    select 字段1,...字段n from 表名 order by 字段名1,...字段名n; # 默认asc升序
    
    
    select 字段1,...字段n from 表名 order by 字段名1,...字段名n desc;  # 降序
    
    
    select 字段1,...字段n from 表名 order by 字段名1 desc,...字段名n asc;  # 字段1降序,字段n升序
    
    
    

    7. limit

    # 模式
    """
    	select 字段1,...字段n from 表名 order by 字段名1,...字段名n limit 10;
    """
    
    
    
    # 作用
    """
    	1. 用于限制显示的记录数
    	2. limit [start,] count;
    		start 开始位置,第一行,start为0
    		count 显示条数
    		不指定start时,则从第一条开始显示
    	3. limit可用于分页
    		分页原理:先查询总数据条数,设为a
    		确定每页数量b,
    		总页数为c = a / b 如果除不尽则需要加1  例如 10 / 3 正确页数为4
    		查询语句的起始位置为s = 当前页数d 减去1 乘以每页数量
    		即  s =  (d - 1) * b
    		语句为:select*from table_name limit s,b
    """
    
    
    
    # 练习
    """
    	1. 查看前三人
    	select *from emp limit 3;
    	2. 查看工资最高的那个人信息
    	select *from emp order by salary desc limit 1;
    	3. 指定起始位置,查看id为3-6的人
    	select *from emp limit 2,4;
    	
    """
    
    
  • 相关阅读:
    [QT]
    [QT]
    企业内搜索引擎项目(一):架构
    Muduo网络库实战(二):实现服务器与客户端的连接
    Muduo网络库实战(一):安装和配置
    Xapian实战(一):环境搭建 + 简介
    Centos 6.5升级gcc : 源码安装 + rpm安装
    Hadoop学习笔记(二)——插件安装和使用(Hadoop Eclipse)
    Hadoop学习笔记(三) ——HDFS
    Hadoop学习笔记(一)——安装与配置
  • 原文地址:https://www.cnblogs.com/plf-Jack/p/11189890.html
Copyright © 2011-2022 走看看