select * from info;
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;
count() 统计有多少个行
sum()列的值相加,求和
avg()列的值求平均数
max()过滤出列的最大值
min() 过滤出列的最小值
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 '杨_';
在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;
表的别名 不能和数据库中的其他的表名冲突
列的别名在结果中可以显示,但是表的别名在结果中没有显示,只能用于查询