• MySQL的高阶语句


    数据库的权限一般很小,工作中使用最多的场景就是查

    排序、分组、子查询、视图、多表连接查询(左连接、右连接、内连接)

    create TABLE info (

    id int(4) primary key,

    NAME varchar(5) not null,

    score decimal(5,2),

    address varchar(20),

    sex char(3) not null

    );

    select * from info;

    ①使用select语句,使用order by进行排序

    ASC:升序排列

    desc  倒序排列,降序排列。需要添加

    select id,name from info order by id;  升序

    select id,name from info order by id DESC;  降序

    select id,name,score from info order by score DESC;  降序,by后面跟上要排序的列

    排序尽可能用数字

    order by 结合where条件过滤

    查姓名 成绩,根据地址=东京西路。按照分数进行降序排列

    select name,score from info where address='东京西路' order by score desc;

    查id 姓名 成绩,根据性别=女。按照id进行降序排列

    select id,name,score from info where sex='女' order by id desc;

    select id,name,score from info where sex='女' ORDER BY Score desc,id desc;

    只有第一个参数出现相同值时,第二个参数才会按照要求排序

    区间判断查询和去重查询

    AND或者or进行条件判断

    and 两边都要为真,or两边一个为真即可

    大于70或者小于等于90

    select * from info where score > 70 and score <= 90;

    成绩大于80或者小于90

    select * from info where score > 80 or score < 90;

    嵌套条件

    select * from info where score > 70 and ( score > 75 and score <90);

    select * from info where score > 70 or ( score > 75 and score <90);

    分组

    用嵌套条件,满足性别是男,然后筛选成绩80--90之间

    select * from info where sex='男' and (score > 80  and score <90);

    去重查询(面试会问)

    select DISTINCT sex from info;

    select DISTINCT address from info;

    根据addres去重,然后过滤出成绩=90且,性别是男

    select DISTINCT address from info where sex='男' and (score =90);

    根据addres、name、score去重,然后过滤出成绩=90且,性别是男

    select DISTINCT address,name,score from info where sex='男' and (score =90);

    对结果进行分组查询group by语句

    一般结合聚合函数一起使用

    count() 统计有几行

    sum()  列的值相加求和

    avg()  列的值求平均数

    max()  过滤出列的最大值

    min()  过滤出列的最小值

    分组的时候可以按照一个字段,也可以按照多个字段对结果进行分组处理

    select count(name),sex from info group by sex;

    根据where条件筛选,score >=80

    select count(name),sex,score from info where score >=80 group by sex;

    求成绩的和

    求和: 以地址为分组,对score求和。

    select sum(score),address from info group by address;

    算出男生女生的平均成绩

    select avg(score),sex from info group by sex;

    分别算出男生组和女生组的成绩最低的姓名

    select min(score),sex,NAME from info group by sex;

    group by实现条件过滤,后面跟上having语句实现条件过滤。

    select avg(score),address from info group by address HAVING avg(score) > 60;

    按照地址分组,求成绩的平均值,然后>50,按照id的降序排列

    select avg(score),address,id from info group by address HAVING avg(score) > 50 order by id desc;

    统计name的行数,计算出学生的个数,把成绩也查出来,然后按照统计出来的学生个数,升序排列。按照地址分组。学生的成绩>=70

    select count(name),address,score,name from info group by address HAVING score >=70 order by count(name);

    按照性别分组,求出男生和女生的最大成绩,最大成绩是否超过75分。满足条件的过滤出来。

    select max(score),sex from info group by sex HAVING max(score) >=75;

    使用聚合函数必须使用group by

    分组的条件要选用有多个重复值的列

    过滤条件要用having语句过滤

    limit限制输出的结果记录。查看表中的指定行

    只看前三行

    select * from info limit 3;

    查看2-5行

    select * from info limit 1,4;

    倒叙查看最后三行

    select * from info order by id desc limit 3;

    通配符:主要用于替换字符串中的部分字符,通过部分字符的匹配,将相关的结果查询出来

    通配符和like一起使用,要使用where语句一起完成查询

    通配符:

    %  表示0个、1个或者多个

    _   表示单个字符

    以四为开头

    select * from info where address like '四%';

    西为结尾

    select * from info where address like '%西';

    包含川

    select * from info where address like '%川%';

    select * from info where name like '小_';

    select * from info where name like '小_小';

    select * from info where name like '__大';

    以四为开头,匹配后面两个字符

    select * from info where address like '四%__';

    通配符可以一起使用

    设置别名alias

    简写AS

    在MySQL查询时,表的名字或者字段名太长,可以用别名来进行替代,方便书写。可以增强可读性

    select name 姓名,score 成绩 from info;

    select name as 姓名,score as 成绩 from info as fmh where name='杨dd' and id = fmh.id;

    as语句创表

    CREATE table test as select * from info;

    select * from test;

    desc test;

    desc info;

    使用as复制表,约束不会被复制过来(as复制的没有主键)

    可以给表起别名,但是要注意别名不能和数据库中其他的表名冲突

    列的别名在结果中可以显示,但是表的别名在结果中没有显示,只能用于查询。

  • 相关阅读:
    Unity之ShaderGraph如何实现冰冻效果
    开创性的区块链操作系统项目——内容所有权和国际象棋
    urllib.error.URLError 提示错误解决方法和爬虫基本知识
    你安全吗?丨通过IP地址如何查到实际地址?
    为什么吃奶粉的宝宝比吃母乳宝宝更胖?
    高斯混合模型GMM及EM迭代求解算法(含代码实现)
    Python 运行代码
    SpringCloud原理-OpenFeign篇(三、FeignClient的动态代理原理)
    【SSH远程登录长时间连接后容易出现自动断开的解决方案】
    UE 虚幻引擎,项目结构
  • 原文地址:https://blog.csdn.net/a91888888/article/details/134264342