1.查询时起别名
select name [as] 名字 from 表名select name [as] 名字 from student;
2.查询时合并列
select name, math, english, math+english [as] 总分 from student;
3.查询时去除重复
select distinct name from student;
4.条件查询
select * from student where math=50 or math=85;select * from student where math in (50, 85);
5.比较条件(> < >= <= = <>(不等于))
select * from student where math>50 and math<96;select * from student where math<>65;select * from student where math is null;
6.模糊查询(like)
% _ 占位符 %:为任意字符 _:占一个字符
select * from student where name like '%红';select * from student where name like '_能';
7.聚合函数
1. count 计算个数(一般使用非空列表名或者count(*))
2.avg 计算平局数
3.min 最小值
4.max 最大值
5.sum 求和
注意:聚合函数的计算, 排除null的值
解决办法:1.选取非空的列表
2.使用ifnull(列表名, 0)
-- 求和select sum(math) from student;-- 求个数select count(*) from stduent;
8.分组查询
group by
hangving 使用在group 的后面
where 使用在group前
// 得出每个性别人的总数Select gender, count as 总数 from class group by gender;select gender, count(*) as 总数 from class where math > 60 group by gender having 总数 > 1;
select * from class limit 0,3;select * from class limit 3,3;select * from class limit 6,3;
10.级联操作
-- 添加级联操作-- 语法:alter table 表名 add constraint 外键名foreign key (外键关键字) references 表名(列表名) on update cascade;-- 级联分类1. on update cascade; 更新级联2. on delete cascade; 删除级联
文章转载自南京的萝卜,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




