mysql的高阶语句

select * from info;

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

ASC :升序。可以不加

desc :降序排列,需要添加

order by 结合 where条件过滤

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

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

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

SELECT id,name,score from info where sex='女' ORDER BY id DESC,score desc;

区间判断

小于等于90 大约70

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

大于80或者小于90

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

嵌套判断

以 and 后面为准

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

以or 前面为准

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

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

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

去重查询

Select distinct address from info;

select distinct address from info;

select 的求值

count() 统计有多少个行

sum()列的值相加,求和

avg()列的值求平均数

max()过滤出列的最大值

min() 过滤出列的最小值

group by 分组处理

select * from info where score >=80 group by sex;

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

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

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

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

select * from info where score >=80 group by sex;

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

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

Group by 实现条件过滤 后面需要跟上having语句实现条件过滤

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

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

select*from info;
按照性别分组,求出男生和女生的最大成绩,最大成绩是否超过75分。满足条件的过滤出来。
select max (score) ,sex from info group by sex having max (score) > 75;
使用聚合函数必须要加Igroup by分组的条件,要选用有多个重复值的列。过滤条件要用having语 句过滤

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

#第6行往下数3行

select * from info limit 5,3;

#快速的查看最后3行

Select * from info order by id desc limit3;

通配符:

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

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

%:表示0个 1个 多个

_: 表示单个字符

Select * from info where address like ‘%西%’;

以山为开头 c%

以什么为结尾 %c

表示内容包含 %c%

Select * from info where name like '杨_';

设置别名: alias >> AS

在mysql 查询时表的名字或者字段名长,可以使用别名替代,

Select name姓名,score 成绩 from info;

创建as语句时复制表 主键会消失复制的是数据结构(约束不会被复制过来)

Create table test as select * from info;

Select * from test1;

Desc test1;

指定大于60的

Create table test as select * from info where score >=60;

表的别名 不能和数据库中的其他的表名冲突

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值