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复制的没有主键)

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

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

相关推荐
Channing Lewis27 分钟前
sql server如何创建表导入excel的数据
数据库·oracle·excel
秃头摸鱼侠28 分钟前
MySQL安装与配置
数据库·mysql·adb
UGOTNOSHOT32 分钟前
每日八股文6.3
数据库·sql
行云流水行云流水1 小时前
数据库、数据仓库、数据中台、数据湖相关概念
数据库·数据仓库
John Song1 小时前
Redis 集群批量删除key报错 CROSSSLOT Keys in request don‘t hash to the same slot
数据库·redis·哈希算法
IvanCodes1 小时前
七、Sqoop Job:简化与自动化数据迁移任务及免密执行
大数据·数据库·hadoop·sqoop
tonexuan1 小时前
MySQL 8.0 绿色版安装和配置过程
数据库·mysql
JohnYan1 小时前
工作笔记- 记一次MySQL数据移植表空间错误排除
数据库·后端·mysql
我最厉害。,。2 小时前
Windows权限提升篇&数据库篇&MYSQL&MSSQL&ORACLE&自动化项目
数据库·mysql·sqlserver
远方16092 小时前
20-Oracle 23 ai free Database Sharding-特性验证
数据库·人工智能·oracle