整理常用SQL语句(二)

修改字段

alter table table_name modify propName propType; # 修改字段的类型
alter table table_name change pnameOld pnameNew pTypeOld; # 修改字段名
alter table table_name change pnameOld pnameNew PTypeNew; # 同时修改字段名和类型

查询

给字段取别名

-- 别名

select name as 姓名,gender as 性别 from student_table;

-- 或者

select name 姓名, gender 性别 from student_table;

给表取别名

select * from student_table as stu;

去重 distinct

selet distinct gender from student_table;

-- 只显示男、女

where 条件

select * from student_table where gender = '男';

select name, age from student_table where gender = '男’;

-- '*' 处控制列, where 控制行

比较运算符

=

<
<=

=
!= 、 <>

-- 查询小于30 岁的

select name from student_table where age <= 30;

逻辑运算

and
or
not

模糊查询 like

% 代表任意字符
_ 代表一个任意字符

-- 以孙开头的学生

select * from student_table where name like '孙%';

-- 以姓孙且名字两位的

select * from student_table where name like '孙_';

-- 以乔结尾

select * from sudent_table where name like '%乔';

-- 名字中有白

select * from student_table where name like '%白%';

-- 名字两个字

select * from student_table where name like '__';

范围查找 in 、between... and...(闭区间)

-- 老家是北京、上海

select * from student_table where hometown = '北京' or homtown = '上海';

select * from student_table where hometown in ('北京', '上海');

-- 年龄在25到30

select * from student_table where ages >=25 and ages <= 30;

select * form student_table where ages between 25 and 30;

空判断: is null

-- 查找身份证为空的学生

select * from student_table where id_card is null;

修改

update student_table set field1 = value where condition;

update student_table set class = '1班' where name like '孙%';

删除

delete from tb_name where conditions;

delete from student_table where class = '1班';

排序 order by

order by 字段 asc(从小到大,默认)\ desc(从大到小)

-- 年龄从小到大

select * from student_table order by age;

-- 年龄从小到大,年龄相同按学号

select * from student_table order by age , id;

-- 男学生从大到小

select * from student_table where gender = '' order by age desc;

聚合函数

count()

-- 查询学生总数

select count(*) from student_table;

-- 查询班级数

select count(distinct class) from student_table;

-- 女生数量

select count(*) from student_table where gender = '';

max()

-- 年龄最大

select max(age) from student_table;

-- 年龄最大的女

select max(age) from student_table where gender = '0';

-- !!! 聚合函数不能用到where 后面的条件

min()

sum()

-- 年龄总和

select sum(age) from student_table;

avg()

-- 平均年龄

select avg(age) from student_table;

-- avg 自动忽略null,null 作为字母参与计算

-- 查询最大年龄、和最小年龄

select max(age) 最大年龄, min(age) 最小年龄 from student_table;

数据分组 group by

-- 分别查询男女同学的数量

select count(*) from studens group by gender;

select sex, count(*) from studens group by gender;

-- !!! gourp by 是配合 聚合函数使用的

-- 1班男女数量

select sex, count(*) from student_table where class = '' group by gender;

select count(*) from st_table where conditions group by field order by field;

having

-- 查询男生总数
-- 筛选 聚合

select count(*) from student_table where gender='';

-- 分组 聚合 筛选

select count(*) from student_table group by gender having gender = '';

-- !!! having 总是在group by 后面

-- !!! having 后面可以用聚合函数
-- 求班级大于3人的名字

select class from student_table group by having count(*) > 3;

-- where 是对原始数据进行处理,后面不能用聚合函数
-- having 是对筛选出来的数据进行处理 ,可以用聚合函数

分页 limit start, num

select * from table_name limit 0, 3;

select * from tbale_name limit 3; # default start = 0

-- limit 总是出现在sql 语句的最后;

-- 查询年龄最大学生姓名

select name from student_table order by age desc limit 1;

-- 求第n页数据

select name from student_table limit (n-1)*m, m;

posted @ 2020-07-15 02:43  阳澄湖大水怪  阅读(117)  评论(0)    收藏  举报